Open second figure from GUI

3 visualizzazioni (ultimi 30 giorni)
Carlos
Carlos il 31 Lug 2014
Risposto: Adam il 1 Ago 2014
Hi,
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

Risposte (2)

Ben11
Ben11 il 31 Lug 2014
Modificato: Ben11 il 31 Lug 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.
else
% if user unchecks the checkbox, do whatever you want.
end
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
Adam il 1 Ago 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.

Categorie

Scopri di più su Specifying Target for Graphics Output in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by