如何让varargout从Matlab GUI中的按钮输出文本?

时间:2015-06-19 02:35:40

标签: matlab matlab-guide

我正在尝试使用GUIDE创建一个GUI,它允许用户选择两个按钮中的一个。按钮中的字符串每次都会变化(我还没有自动化),但是当按下任何按钮时,我希望GUI将字符串作为输出变量。

我有代码来显示工作区中的输出,但是在调用OutputFn之前删除了句柄。有关如何解决此问题的任何建议吗?

另外,我想使用' Next'按钮。理想情况下,这应该以一种迭代的方式拉出按钮上显示的下两个文本,但我现在很乐意超越获取输出的障碍。这就是我到目前为止所拥有的: / p>

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

1 个答案:

答案 0 :(得分:0)

我对我的问题有了答案。变更摘要:

  • 在每次回调后添加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)

从帖子体验中学习:

  • 我应该对我的问题更加具体。希望我会 通过更多的经验和学习正确的“行话”来获得更好 使用。
  • 我应该在我的第一篇文章中指出我确实花了 相当多的时间在Matlab GUI上查看多个博客和网站, 指南和guidata。坦率地说,我找到的所有网站都没有解决 我的具体问题。
  • 这篇文章终于让我弄清楚了我的错误: http://www.mathworks.com/matlabcentral/answers/141809-issue-with-gui-editbox-callback