Alexandria
Alexandria

Reputation: 23

How I make a pushbutton invisible in a GUI?

I want to play my wav on percussion background image area with my pushbutton, so i need my pushbutton invisible on my figure window.

My script:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[s,fs]=wavread('filename.wav');
sound(s,fs);

Thankyou..

Upvotes: 2

Views: 653

Answers (3)

NLindros
NLindros

Reputation: 1693

To make your push button invisible when you click it, set visibleto off in the callback function

set(hObject, 'Visible', 'off')

To make it invisible from other parts/functions in your GUI, just replace hObject with the handle of your push button.


Update:

You could make a clickable image and play different sounds for different click positions. Use the callback 'ButtonDownFcn' to trigger at a click event in the image. You can the retrive the position of the click by using the axes property 'CurrentPoint'. This return as 2x3 matrix with x-y-z projected coordinates. But as you are using a 2D plot you could simply pick the first 2 values, read more here.

Then use the x/y coordinates to find out what in the image that the user clicked on and play the sound for that.

A simple example:

% Draw an image
figure()
imHandle = image(imread(figPath));
% Set callback function (target function could have any name)
set(imHandle,'ButtonDownFcn', @ImgClickCB); 

And the callback function (displays the x and y coord.)

function ImgClickCB(hObject, ~)
clickPoint = get( get(hObject,'Parent'), 'CurrentPoint');
fprintf('Clicked at x: %0.f y: %0.f \n', clickPoint(1,1), clickPoint(1,2));

Upvotes: 1

matlabgui
matlabgui

Reputation: 5672

You might want to have a look at this blog entry where I discussed how to manipulate the CData property of uicontrols.

I've added some code below to show a simple example:

f = figure(); % create a figure with an axes on it
pb = uicontrol('Style','checkbox', 'Units','pixels', 'Position',[10 10 300 200], ...
               'Callback',@(a,b)msgbox('play clown!'));
% read some data
data = load ( 'clown' );
% extract out the image
img = data.X;
% convert image to RGB for displaying on checkbox
img = ind2rgb(img,colormap(f)); 
% Set the cdata property of the checkbox to be the image of interest
set(pb, 'CData', img )

The above code creates a figure with an image of a clown which you can click on (this could be your drum). The "button" stays there the whole time you don't need to make it invisible

Note: I use a checkbox instead of a button -> because sometimes a button can have a "border" when its in focus which can detract from the image whereas a checkbox doesn't.

I've copied the image produced below (after I clicked on the button):

enter image description here

Upvotes: 0

Rotem
Rotem

Reputation: 32104

The following example hides, and shows a pushbutton.

I created a sample, without using guide.
You can copy and paste the code into Matlab m file for execution.
Creating GUI without guide tool, better suit Stack Overflow site, because there is no need to attach a fig file.
You better use guide tool, because creating a GUI without it is complicated.

The following code sample hide (and show) pushbutton:

%TestNoGuideHideButton.m
%Create GUI with two buttons, without using GUIDE.
function TestNoGuideHideButton()
    %Create figure.    
    h.fig = figure('position', [800 400 260 80]);

    %Add button, with callback function Button1
    h.buttonOne = uicontrol('style', 'pushbutton',...
        'position',[10 20 100 40], ...
        'string' , 'Button1', ...
        'callback', {@Button1});

    %Add button, with callback function hideButton
    h.buttonTwo = uicontrol('style', 'pushbutton', ...
        'position',[150 20 100 40], ...
        'string' , 'Hide Button1', ...
        'callback', {@hideButton});


    function Button1(hObject, eventdata)
        %Modify color of Button1 to random color.
        set(h.buttonOne, 'BackgroundColor', rand(1, 3));
    end


    function hideButton(hObject, eventdata)
        is_visible = isequal(get(h.buttonOne, 'Visible'), 'on');

        if (is_visible)
            %Hide buttonOne if Visible.
            set(h.buttonOne, 'Visible', 'off');
            set(h.buttonTwo, 'string', 'Show Button1'); %Replace string.
        else
            %Restore buttonOne if hidden.
            set(h.buttonOne, 'Visible', 'on');
            set(h.buttonTwo, 'string', 'Hide Button1'); %Replace string.
        end
    end
end

For the problem you described above, you obviously can't add a button for showing and hiding the other button.

You can restore the button when playing finishes.

You can also add a callback function for the background figure (look for WindowButtonDownFcn in guide).
Pressing anywhere on the figure, triggers the callback, were you can restore the hidden button.


enter image description here

Upvotes: 0

Related Questions