How to return the values of variables in GUI

17 views (last 30 days)
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

Image Analyst
Image Analyst on 1 Oct 2014
Edited: Image Analyst on 1 Oct 2014
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
Walt Kailey
Walt Kailey on 1 May 2017
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
Image Analyst
Image Analyst on 1 May 2017
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)

Jesus GO
Jesus GO on 28 Nov 2017
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!!

Community Treasure Hunt

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

Start Hunting!