如何从MATLAB GUI函数返回多个值

时间:2014-10-31 02:11:29

标签: matlab user-interface return

我一直在尝试从MATLAB中的GUI返回一些带有3个不同按钮组的值,如下例所示,但是没有返回任何值。

我也尝试使用全局变量,但这也不起作用。

任何帮助都将不胜感激。

 function mainUI(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @mainUI_OpeningFcn, ...
                   'gui_OutputFcn',  @mainUI_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 mainUI is made visible.
function mainUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;

guidata(hObject, handles);

uiwait(handles.gui);

% --- Outputs from this function are returned to the command line.
function varargout = mainUI_OutputFcn(hObject, eventdata, handles) 
    varargout{1} = handles.output;

% --- Executes when selected object is changed in SensorLocPanel.
function SensorLocPanel_SelectionChangeFcn(hObject, eventdata, handles)
    switch hObject
        ...
    end

% --- Executes when selected object is changed in StepTypeSel.
function StepTypeSel_SelectionChangeFcn(hObject, eventdata, handles)
    switch hObject
        ...
    end

% --- Executes when selected object is changed in FileSelectPanel.
function FileSelectPanel_SelectionChangeFcn(hObject, eventdata, handles)
    switch hObject
        ...
    end

    % --- Executes on button press in buttonRun.
function buttonRun_Callback(hObject, eventdata, handles) 
    clc;
    close all;
%     delete(handles.gui)
    return; % Quit the program

% --- Executes during object creation, after setting all properties.
function buttonChest_CreateFcn(hObject, eventdata, handles)


% --- Executes during object deletion, before destroying properties.
function buttonChest_DeleteFcn(hObject, eventdata, handles)

% --- Executes when user attempts to close gui.
function gui_CloseRequestFcn(hObject, eventdata, handles)
    if isequal(get(hObject,'waitstatus'),'waiting')
        uiresume(hObject);
        guidata(hObject,handles);
    else
        % The GUI is no longer waiting, so destroy it now.
        delete(hObject);
    end

1 个答案:

答案 0 :(得分:0)

这个gui是用GUIDE完成的吗?在这种情况下,我建议以下video tutorial在关闭时从gui返回值。如果你想在gui运行时将某些内容返回到其他工作区(我不知道为什么,但假设你有正当理由)我认为全局变量至少是一个好方法。但请注意,您需要写一行

global <gVars>

在要使用全局变量的每个函数中。全局变量不能像某些语言那样被访问,它们的行为更像外部变量。但是,来自全局变量的数据当然存储在全局工作空间中,这意味着在使用全局变量的每个函数中都可以访问当前值。

但是,如果我没记错的话,图中有一个名为UserData的字段可以记录一些用户输入数据。可以在那里定义一个包含要更改的所有变量的结构。当您需要其他工作区中的数据时,您只需从该字段中读取数据。