How to have multiple GUI functions access varargin?

1 view (last 30 days)
Is there a way to have all the functions in a GUIDE-based GUI access the input arguments to the function? I've done this by creating global variables for each of the input arguments so all the other functions have access to them. I'm wondering if there is a different way without creating globals.
Thanks!

Answers (1)

Geoff Hayes
Geoff Hayes on 7 Mar 2016
John - rather than using global variables, why not save the input parameters to the handles structure? Presumably, you are accessing your input parameters in the OpeningFcn of your GUI so you could do something like
function GuiSaveDataExample_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for GuiSaveDataExample
handles.output = hObject;
if length(varargin) > 1
handles.input1 = varargin{1};
handles.input2 = varargin{2};
end
% Update handles structure
guidata(hObject, handles);
The order in the above is important - we read the (for example) two input parameters and store each in fields named input1 and input2 (you can name these to whatever they represent) and then call guidata to save the updated handles structure. As the third input to each callback is (typically) handles, then each callback has access to these input parameters.
Try the above and see what happens!

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!