Adi kul
Adi kul

Reputation: 63

Making MATLAB GUI Radio button as figure visibility ON-OFF

I am working on MATLAB GUI for one of my script. My script has Start push button under which I call functions to solve the equation. I have 3 figures in it. So till now my GUI works well. What happens is, when user clicks Start after giving required inputs, the 3 figures pops up one by one and I have specified location to save the figures. It looks nice when loop is small but when loops are big, the calculation time increases so the figure pop up will be come irritating to user as user can not do anything.

I want to introduce a radio button in the GUI which unchecked will disable the visibility of figures. I tried figure('Visible','off') and it worked well but now I want it to link with a radio button.

The radio button callback is:

% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox2

Can anyone help me on who to frame the code to make this radio button to ON-OFF the visibility of figures in push button:

function Start_Callback(hObject, eventdata, handles)
% hObject    handle to Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

Waiting for your help.

Upvotes: 0

Views: 298

Answers (2)

Rotem
Rotem

Reputation: 32084

I assume your main problem is setting visibility of all three figures.
For setting the visibility of specific figure, the best option is to keep handle to that figure.

I recommend to keep the figure handle when opening a new figure:

h1 = figure; %Open new figure, and store figure handle in h1
h2 = figure; %Open new figure, and store figure handle in h2
h3 = figure; %Open new figure, and store figure handle in h3

I think the right place to create (open) the figures in the GUI OpeningFcg function.

When creating new GUI, using guide tool, Matlab generates the following code:

function guiname_OpeningFcg(hObject, evevntdata, handles, varargin)  
...
handles.output = hObject;

guidata(hObject, handles);
...

After code line guidata(hObject, handles);, you can create your figures.
Store the figure handles in handles structure for later usage:

function guiname_OpeningFcg(hObject, evevntdata, handles, varargin) 
...
handles.output = hObject;

guidata(hObject, handles);

h1 = figure; %Open first figure, and store figure handle in h1
h2 = figure; %Open second figure, and store figure handle in h2
h3 = figure; %Open third figure, and store figure handle in h3

handles.h1 = h1; %Store handle to first figure.
handles.h2 = h2; %Store handle to second figure.
handles.h2 = h2; %Store handle to third figure.

In the callback function, use the following code to set visibility:

% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)

val = get(handles.checkbox2, 'Value');

if (val)
    vis = 'on';
else
    vis = 'off';
end

set(handles.h1, 'Visible', vis); %Set Visibility of first figure.
set(handles.h2, 'Visible', vis); %Set Visibility of second figure.
set(handles.h3, 'Visible', vis); %Set Visibility of third figure.

Upvotes: 1

serial
serial

Reputation: 437

In your Start_Callback check the status of the checkbox/radiobutton and create a new variable for later use:

if get(handles.checkbox2, 'Value')
    Visibility = 'on';
else 
    Visibility = 'off';
end

now create your figures:

figure('Visible', Visibility)

depending on the value of Visibility they will be visible or not

Upvotes: 1

Related Questions