How to return the values of variables in GUI

18 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2014-10-1
编辑:Image Analyst 2014-10-1
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 个评论
Walt Kailey
Walt Kailey 2017-5-1
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 2017-5-1
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.

请先登录,再进行评论。

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by