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)
handles.output = hObject;
guidata(hObject, handles);
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
to
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)
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.