Open second figure from GUI

1 次查看(过去 30 天)
Carlos
Carlos 2014-7-31
回答: Adam 2014-8-1
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

回答(2 个)

Ben11
Ben11 2014-7-31
编辑:Ben11 2014-7-31
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 2014-8-1
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.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by