如何让 varargout 从 Matlab GUI 中的按钮输出文本?
How to get varargout to output text from pushbuttons in Matlab GUI?
我正在尝试使用 GUIDE 创建一个 GUI,它允许用户从两个按钮中选择一个。按钮中的字符串每次都会有所不同(我还没有自动执行此操作),但是当按下任何按钮时,我希望 GUI 将字符串作为输出变量输出。
我有在工作区中显示输出的代码,但句柄在调用 OutputFn 之前被删除。关于如何解决这个问题有什么建议吗?
此外,我想使用 'Next' 按钮。理想情况下,这应该以迭代的方式拉出要在按钮上显示的下两个文本,但我很乐意克服获取 now.Here 输出的障碍,这是我目前所拥有的:
function varargout = Select_A_B(varargin)
% SELECT_A_B MATLAB code for Select_A_B.fig
% SELECT_A_B, by itself, creates a new SELECT_A_B or raises the existing
% singleton*.
%
% H = SELECT_A_B returns the handle to a new SELECT_A_B or the handle to
% the existing singleton*.
%
% SELECT_A_B('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SELECT_A_B.M with the given input arguments.
%
% SELECT_A_B('Property','Value',...) creates a new SELECT_A_B or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Select_A_B_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Select_A_B_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Select_A_B
% Last Modified by GUIDE v2.5 18-Jun-2015 15:12:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Select_A_B_OpeningFcn, ...
'gui_OutputFcn', @Select_A_B_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Select_A_B is made visible.
function Select_A_B_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 Select_A_B (see VARARGIN)
% Choose default command line output for Select_A_B
handles.output = hObject;
handles.string = '';
if isempty(varargin)
varargin{1} = 1;
varargin{2} = 1;
end
text1 = varargin{1};
text2 = varargin{2};
A = {'Apple';'Orange'};
B = {'Football';'Basketball'};
set(handles.pushbutton1,'string',A(text1)); % wait until we're ready
set(handles.pushbutton2,'string',B(text2)); % wait until we're ready
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Select_A_B wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Select_A_B_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 default command line output from handles structure
varargout{1} = hObject;
varargout{2} = handles.string;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selectedButton = get(hObject,'String')
set(handles.string,'String',selectedButton);
guidata(hObject, handles);
close(gcf);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selectedButton = get(hObject,'String')
set(handles.string,'String',selectedButton);
guidata(hObject, handles);
close(gcf);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function pushbutton1_CreateFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
我的问题有了答案。更改摘要:
- 在每次回调后添加 uiresume(在 Opening 函数中添加 uiwait 之后)
- 每次回调后删除关闭(gcf);如果图窗关闭,则输出函数无法再访问 guidata 中的 hObject 和句柄
- 创建了一个句柄结构来包含感兴趣的文本并将其保存在
每个回调函数后的 guidata
在输出函数中将 guidata 的输出保存到 .mat 文件
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selectedButton = get(hObject,'String')
handles.string = selectedButton;
guidata(hObject, handles);
uiresume(handles.figure1);
% close(gcf);
function varargout = Select_A_B_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 default command line output from handles structure
varargout{1} = hObject;
varargout{2} = handles.string;
save 'guioutput'
delete(hObject)
post经验教训:
- 我应该更具体地回答我的问题。希望我会
通过更多的经验和学习正确的“行话”变得更好
使用。
- 我应该在我的第一个 post 中指出我确实花了
相当多的时间在 Matlab GUI 上查看多个博客和站点,
指南和guidata。坦率地说,none 我找到的网站地址
我的具体问题。
- 这个 post 是最终帮助我找出错误的那个:
http://www.mathworks.com/matlabcentral/answers/141809-issue-with-gui-editbox-callback
我正在尝试使用 GUIDE 创建一个 GUI,它允许用户从两个按钮中选择一个。按钮中的字符串每次都会有所不同(我还没有自动执行此操作),但是当按下任何按钮时,我希望 GUI 将字符串作为输出变量输出。
我有在工作区中显示输出的代码,但句柄在调用 OutputFn 之前被删除。关于如何解决这个问题有什么建议吗?
此外,我想使用 'Next' 按钮。理想情况下,这应该以迭代的方式拉出要在按钮上显示的下两个文本,但我很乐意克服获取 now.Here 输出的障碍,这是我目前所拥有的:
function varargout = Select_A_B(varargin)
% SELECT_A_B MATLAB code for Select_A_B.fig
% SELECT_A_B, by itself, creates a new SELECT_A_B or raises the existing
% singleton*.
%
% H = SELECT_A_B returns the handle to a new SELECT_A_B or the handle to
% the existing singleton*.
%
% SELECT_A_B('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SELECT_A_B.M with the given input arguments.
%
% SELECT_A_B('Property','Value',...) creates a new SELECT_A_B or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Select_A_B_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Select_A_B_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Select_A_B
% Last Modified by GUIDE v2.5 18-Jun-2015 15:12:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Select_A_B_OpeningFcn, ...
'gui_OutputFcn', @Select_A_B_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Select_A_B is made visible.
function Select_A_B_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 Select_A_B (see VARARGIN)
% Choose default command line output for Select_A_B
handles.output = hObject;
handles.string = '';
if isempty(varargin)
varargin{1} = 1;
varargin{2} = 1;
end
text1 = varargin{1};
text2 = varargin{2};
A = {'Apple';'Orange'};
B = {'Football';'Basketball'};
set(handles.pushbutton1,'string',A(text1)); % wait until we're ready
set(handles.pushbutton2,'string',B(text2)); % wait until we're ready
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Select_A_B wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Select_A_B_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 default command line output from handles structure
varargout{1} = hObject;
varargout{2} = handles.string;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selectedButton = get(hObject,'String')
set(handles.string,'String',selectedButton);
guidata(hObject, handles);
close(gcf);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selectedButton = get(hObject,'String')
set(handles.string,'String',selectedButton);
guidata(hObject, handles);
close(gcf);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function pushbutton1_CreateFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
我的问题有了答案。更改摘要:
- 在每次回调后添加 uiresume(在 Opening 函数中添加 uiwait 之后)
- 每次回调后删除关闭(gcf);如果图窗关闭,则输出函数无法再访问 guidata 中的 hObject 和句柄
- 创建了一个句柄结构来包含感兴趣的文本并将其保存在 每个回调函数后的 guidata
在输出函数中将 guidata 的输出保存到 .mat 文件
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) selectedButton = get(hObject,'String') handles.string = selectedButton; guidata(hObject, handles); uiresume(handles.figure1); % close(gcf);
function varargout = Select_A_B_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 default command line output from handles structure varargout{1} = hObject; varargout{2} = handles.string; save 'guioutput' delete(hObject)
post经验教训:
- 我应该更具体地回答我的问题。希望我会 通过更多的经验和学习正确的“行话”变得更好 使用。
- 我应该在我的第一个 post 中指出我确实花了 相当多的时间在 Matlab GUI 上查看多个博客和站点, 指南和guidata。坦率地说,none 我找到的网站地址 我的具体问题。
- 这个 post 是最终帮助我找出错误的那个: http://www.mathworks.com/matlabcentral/answers/141809-issue-with-gui-editbox-callback