How to return output from a function in a set()?

2 views (last 30 days)
I am now using Matlab GUI and have problem during accessing return values from a function which is set by set().
Situation: I set the windowMotionFcn as below
set(gcf,'WindowButtonMotionFcn',@test);
Function 'test' can return 2 variables (named as var1 and var2).But I dont know how to store them... I have searched in the Internet and could not find any way.
How should I write?
Thank you for your help and kind attention.

Accepted Answer

Geoff Hayes
Geoff Hayes on 31 Jul 2014
Myrick - you could try saving the the output from test to the GUI handles structure which will them be accessible in most other functions (callbacks, etc.) of the GUI using guidata. I will assume that you are using GUIDE to create your GUI, and that your line of code
set(gcf,'WindowButtonMotionFcn',@test);
is called in the Opening function of the GUI. (These assumptions may not necessarily be true for your software, but they are for the test app that I created.)
The signature for the test function can be written simply
function test(varargin)
where the output variables have been removed from the signature. This may be similar to your function (less the return/output variables). Now do the work as before until the var1 and var2 variables have been set. Then add the following code
% the first input parameter to the variable argument input (varargin) array
% should be the figure handle
figHandle = varargin{1};
% get the handles associated with the figure
handles = guidata(figHandle);
% set the test output to a field in the handles structure
handles.testOutput = [var1 var2];
% save the data
guidata(figHandle,handles);
Now, if some other callback needs this output data, it can retrieve it from the testOutput field of the handles structure that is typically passed as the third input parameter to most callbacks. Else, something similar to handles = guidata(figHandle); can be used to get the data.
Try the above and see what happens!

More Answers (0)

Categories

Find more on Graphics Object Programming 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!