How do I modify a GUIDE-generated GUI to accept input arguments?

3 Ansichten (letzte 30 Tage)
What changes do I need to make to a GUI program file created by GUIDE to allow it to accept input arguments, perhaps a structure to be used for initialization?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 8 Feb. 2018
For MATLAB 6.5 (R13) and later releases:
In MATLAB, modify the GUI's OpeningFcn to allow it to accept input arguments. The input arguments you provide to the GUI are accessible from the VARARGIN variable in the OpeningFcn. For example, assuming I pass a structure with a field named String to my GUI and have a static text box with the Tag StaticText, this OpeningFcn will set the String property of the StaticText text box to contain the String field of the structure:
 
function samplegui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to samplegui (see VARARGIN)
% Choose default command line output for samplegui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes samplegui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% Handle no structure being passed to GUI
if nargin<4 | ~isstruct(varargin{1})
set(handles.StaticText,'String','Default String')
else
set(handles.StaticText,'String',varargin{1}.String)
end
FOR MATLAB 6.0 (R12) and MATLAB 6.1 (R12.1):
For MATLAB 6.0 (R12) and MATLAB 6.1 (R12.1), you will need to modify the initialization code of the GUI. Create the GUI, then change line 8 of the program file generated by GUIDE from
 
if nargin==0
to
 
if nargin<=N
where N is the number of input arguments you wish your GUI to accept. Setting N too high is not recommended; it can prevent the GUI from recognizing your callbacks correctly. Next, add a call to your initialization function on line 18 of the file, between the call to the GUIDATA function and the specification of output arguments. For example, if the function Initialize is
 
function varargout = Initialize(h, eventdata, handles, varargin)
% Initialization function
if nargin<4 | ~isstruct(varargin{1})
set(handles.StaticText,'String','Default String')
else
InputStruct=varargin{1};
set(handles.StaticText,'String',varargin{1}.String)
end
then lines 17-19 of the GUI will read:
 
guidata(fig, handles);
Initialize(fig, [], handles, varargin{:})
if nargout > 0
FOR MATLAB 5.3 (R11):
For MATLAB 5.3 (R11), create your GUI and modify the program file by adding "varargin" between the parentheses on the first line of the file and calling your initialization function anywhere in the file after all the components (i.e. any uicontrols you want to manipulate) have been created. For example, adding in the following initialization subfunction to the GUI program file
 
function Initialize(textboxhandle, initstring)
set(textboxhandle, 'String', initstring)
and adding the following code immediately after the command which creates the static text box in the file
 
if nargin==0 | ~isstruct(varargin{1})
Initialize(h1, 'Default String')
else
Initialize(h1, varargin{1}.String)
end
assuming that h1 is the handle of the static text box.

Weitere Antworten (1)

Noam Greenboim
Noam Greenboim am 15 Mai 2016
Bearbeitet: Noam Greenboim am 15 Mai 2016
Here's another hack:
Graphic elements have a field called "UserData". You can stote all the inputs in this elemet, and during the GUI run, they are accessible.
In order to assign data to UserData field, call the GUI with the following:
MY_GUI('UserData',my_data)
where MY_GUI is the GUI, generated by GUIDE, and my_data is the data that you want to store. Note that the code line above also runs the GUI.
During the running of the GUI, you can access that field in:
get(handles.MY_GUI,'UserData')

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by