How do I modify a GUIDE-generated GUI to accept input arguments?
조회 수: 7 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2013년 7월 26일
편집: Noam Greenboim
2016년 5월 15일
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?
채택된 답변
MathWorks Support Team
2018년 2월 8일
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.
댓글 수: 0
추가 답변 (1개)
Noam Greenboim
2016년 5월 15일
편집: Noam Greenboim
2016년 5월 15일
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')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!