How to return the values of variables in GUI

조회 수: 32 (최근 30일)
Khanh
Khanh 2014년 10월 1일
답변: Jesus GO 2017년 11월 28일
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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by