How to return the values of variables in GUI

Hi all,
I'm new to GUI and learning from GUI examples in MATLAB's help.
When I type a variable in command window, it shows that the variable is not valid.
As a part of GUI example in MATLAB help, how could I return the value of the variable 'contents' in the following code?
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
contents = get(hObject,'String');
selectedText = contents{get(hObject,'Value')};
colormapStatus = [selectedText ' colormap'];
set(handles.textStatus, 'string', colormapStatus);
colormap(selectedText)
Thanks.
Khanh

 Accepted Answer

Set varargout in the output function to whatever you want.
% --- Outputs from this function are returned to the command line.
function varargout = controlsuite_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global contents; % Whatever variable you want....
try
fprintf(1, 'In controlsuite_OutputFcn\n');
varargout{1} = contents;
catch ME
errorMessage = sprintf('Error in function controlsuite_OutputFcn.\n\nError Message:\n%s', ME.message);
fprintf('%s\n', errorMessage);
WarnUser(errorMessage);
end
return; % from controlsuite_OutputFcn
You can pass out more by setting varargout{2}, etc. Be sure to make contents global in the function where you assign it also.

7 Comments

I deleted the original function:
function varargout = controlsuite_OutputFcn(hObject, eventdata, handles)
and replaced it by yours. But the variable (contents) assigned to varargout{1} was nil (the command window showed varargout as {[]}).
Did I do wrong something?
How did you call controlsuite()? Was it just by typing int in the command window, or by calling it from another function (if so, give the line of code you used), or by clicking the green run triangle? Where exactly is the output argument supposed to go? And, inside the outputfcn did you set a breakpoint and make sure the variable has a value?
I run controlsuite() by hitting the green triangle in MATLAB editor. I set no break points in the code. I do nothing except replace "function varargout=controlsuite_OutputFcn..." by yours.
But when I assigned a value to "contents" variable in the above function, it worked. I marked where I edited the code in the attached .m file in this comment.
I can't run this without the associated controlsuite.fig file. But if you want to return contents, then you're going to have to declare it global in all the other functions where you used it. Otherwise there is no global value for it and when you execute the output function it will create one with a null value so you're not going to return anything but null.
I added global variable (contents) in the function which includes it, its output was showed in the command window. It seemed to work. Thanks.
The answer leaves several things unclear. I have a callback that belongs to the OK button of my dialogue. What relationship does that callback have to your 'Output_fcn' above? And how do I actually receive the information that 'Output_fcn' put into varargou? To me, this answer is still unclear, I'm afraid. I'm left guessing about how to stitch all this stuff together.
Thanks
Try putting fprintf's in the OpeningFcn and OutputFcn and your OK and Exit button callbacks to see the order that they get called in. You'll see the OutputFcn gets called both at startup and shutdown. You don't need to use handles.output. You can load as many variables you want into the cell array called varargout and they will all be returned to the calling program.

Sign in to comment.

More Answers (1)

First, you need to make the GUI wait for an user action before giving outputs:
https://blogs.mathworks.com/videos/2010/02/12/advanced-getting-an-output-from-a-guide-gui/
To use a global variable, define your handles in the opening function:
function Button_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 Button (see VARARGIN)
handles.contents=0;
Then, use "handles.contents" as variable on your bottom function, and extract the results with "guidata" at the end of your function:
popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to plot2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.contents=3*2;
guidata(hObject,handles)
Then, define your output function as follows:
function varargout = Button_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get outputs from the program
varargout{1} = handles.zoomx1
Good luck!!

Asked:

on 1 Oct 2014

Answered:

on 28 Nov 2017

Community Treasure Hunt

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

Start Hunting!