MATLAB: How to return the values of variables in GUI

guidevariable 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

Best 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.
Related Question