How to make 2 gui sharing the same handles structure

Hello,
I am pretty happy with my way to build up GUI with matlab except that I would like have the possibility to not have "one thousand" edit boxes, or else, impeding my camera display for instance: have to reduce the size of my display for including edit boxes; and in general it is something I would like to do for other hardware control: one window/figure per hardware.
What I would like is to be able, by a pushbutton for exemple, to open an other window which contains a bunch of parameters to set. I can either let it open while it is still updating my main GUI and these values can be initialised from the main too.
Thanks for your suggestions.

 Accepted Answer

If you are using GUIDE to build your applications, then my suggestion would be to create a separate "Parameters" GUI application that opens with the callback for the push-button. You can pass the handles variable and/or just the necessary separate variables input the opening function of the "Parameters" application. After completing your adjustments to the parameters, then you can pass the necessary variables back to the main GUI with a push-button or closing function callback.
If you are using App Designer this becomes a bit more straight forward and can be accomplished by using the tab-panel object and placing your parameter edit boxes on their own tab window.

6 Comments

Thank you for your answer. I am still using guide as some of the functionallity are not up to date with app designer (multi-threaded video acquisition for instance).
I have already tried what you are suggesting and my issue is that even though i can get my handles structure from the main GUI in the second GUI, I cannot initialize well with handles=handles; and I need to close the second GUI to update the main GUI: it is not really worth at the moment to bother with this strategy (for me).
I thought maybe using a guidata with different hObject for each GUI, but in need some guidance.
Overall, It is really an equivalent to tab-panel but with windows that I can hid or close etc.
Tutu,
It appears that you are trying to assign the handles variable from the first GUI directly to that of the second, which does not work that way. Think of the handles variable as that which defines the app and all of its objects (children). Try instead to create a field within the handles structures. Something like handles.Parameters, where you can save and transfer this new structure between both applications.
% Initialize you new Parameters structure in your main app
handles.Parameters = struct('Param1',val1,'Param2',val2,...,'Paramn',valn)
Call second app, containing your parameter adjustment boxes, from the main app using an input argument.
handles.Parameters = UpdateParams_GUI(handles.Parameters);
The opening function from second app will have the input argument saved in varargin
function UpdateParams_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% Verify that varargin contains data. Ends function if empty
if ~isempty(varargin)
P = varargin{1};
else
return
end
% Update your edit boxe values with the appropriate parameter values.
handles.EditBox_Param1.Value = P.Param1;
handles.EditBox_Param2.Value = P.Param2;
...
handles.EditBox_Param2.Value = P.Param2;
% Make a copy of the orginal parameters structure for cases when you close Parameters App
% or 'Cancel' changes.
handles.Parameters = P; % Using the same variable
...
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes main wait for user response (see UIRESUME)
uiwait(hObject)
If you use names for your edit boxes that are structure field compatible names, then you can simplify the populating step with a loop or with cellfun instead of assigning them one at a time.
% Example of how to use cellfun to update multiple parameters using the edit box names
% from above.
flds = fieldnames(P);
cellfun(@(x) set(handles.(['EditBox_',x]),'Value',P.(x)),flds);
Setup buttons OK and Canel buttons to accept/cancel changes made to the parameters
%%
function pb_OK_Callback(hObject, eventdata, handles)
% Get and assign the parameters values to the output variable
P = cellfun(@(x) get(handles.(['EditBox_',x]),'Value'),flds);
handles.output = handles.P;
% Update handles data
guidata(hObject,handles)
% Use UIRESUME instead of delete because the OutputFcn needs to get the updated handles structure.
uiresume(handles.figure_UpdateParams_GUI);
%%
function pb_Cancel_Callback(hObject, eventdata, handles)
% Assign the original parameters values to the output variable
handles.output = handles.P;
% Update handles data
guidata(hObject,handles)
% Use UIRESUME instead of delete because the OutputFcn needs to get the updated handles structure.
uiresume(handles.figure_UpdateParams_GUI);
Then setup your closing and output callback functions
%%
function varargout = UpdateParams_GUI_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% The figure is deleted
delete(hObject)
%%
function figure_UpdateParams_GUI_CloseRequestFcn(hObject, eventdata, handles)
% Resumes to exit gui if 'Close' was pressed
if isequal(get(hObject,'waitstatus'),'waiting')
% Assign the original parameters values to the output variable
handles.output = handles.P;
% Update handles data
guidata(hObject,handles)
uiresume(hObject)
else
% The GUI is no longer waiting and is closed
delete(hObject)
end
Thank you very much for your answer Allen.
I have checked that the data are well passed to the sub-gui. However, I cannot make the outputs to be passed to the main-gui.
I get this error:
Dot indexing is not supported for variables of this type.
Error in subfig>subfig_OutputFcn (line 90)
varargout{1} = handles.Parameters;
Error in gui_mainfcn (line 262)
[varargout{1:nargout}] = feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in subfig (line 40)
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
Error in multiplewindows>pushbutton1_Callback (line 119)
a=subfig(handles.Parameters);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in multiplewindows (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)multiplewindows('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
I have attached my mockup gui for demonstration.
I am not sure to follow the use of uiresume.
Thank you again for your precious help.
Tutu,
While trying to find documentation describing how the uiwait and uiresume functions are utilized within GUIDE GUIs, I came across the discussion in the link below. I have not tried to use this method, but have some familiarity with using setappdata and getappdata functions and think this may be a more simple approach.
setappdata(0,'MyStruct',Struct)
getappdata(0,'MyStruct')
Thank you for the link. I think I can work that out.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Tags

Asked:

on 1 Mar 2021

Commented:

on 22 Mar 2021

Community Treasure Hunt

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

Start Hunting!