Open second figure from GUI

4 views (last 30 days)
Carlos on 31 Jul 2014
Answered: Adam on 1 Aug 2014
I am creating a GUI and one of the features I want allow for is when the user selects a checkbox, it will open a second figure to allow for input.
The input into the second window will then need to be input back into the code for the primary GUI.
I hope this was explained clearly enough.
Thank you

Answers (2)

Ben11 on 31 Jul 2014
Edited: Ben11 on 31 Jul 2014
Hi Carlos,
In order to open a figure (let's say an input dialog box) after selecting a checkbox, you want to write the code in the callback function of that checkbox in your main GUI.
For example, here is the code in the callback function of a checkbox with the tag YourCheckbox:
%Executes on button press in checkbox.
%function YourCheckbox_Callback(hObject, eventdata, handles)
% hObject handle to YourCheckbox (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 YourCheckbox
if get(hObject,'Value') ==1 % if the user checks the checkbox
% Create input dialog
prompt = {'Enter a number:'};
dlg_title = 'Input';
num_lines = 1;
def = {5}; % default values
handles.YourAnswer = inputdlg(prompt,dlg_title,num_lines,def); % YourAnswer will be used in your main GUI. It's now stored in the handles structure of the GUI so you can access it in other callbacks.
guidata(hObject,handles); % update the handles structure in which you stored YourAnswer.
% if user unchecks the checkbox, do whatever you want.
So once the input dialog is closed, the variable YourAnswer is available to use in your main window.
I hope it helps you! If not please ask for more details. Now that I think of it, when you say "figure" do you mean something like a dialog box or rather another GUI?

Adam on 1 Aug 2014
I often use a combination of a class derived from handle (meaning it gets passed by reference) and listeners to achieve communication between multiple GUIs.
It does depend what order of user actions you desire though. For a modal dialog the solution provided by Ben11 works absolutely fine.
If you wish the second GUI to remain open alongside the first (or if it is not modal) then passing into the second GUI an object of a handle-derived class and attaching a listener to this whose callback is in the first GUI works well, but maybe a little complex if you are not familiar with OOP in Matlab.


Find more on Graphics Object Programming in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by