Matlab将数据传递到另一个GUI

时间:2015-03-21 11:59:51

标签: matlab matlab-figure matlab-guide

我是Matlab的新手,请在这个问题上指导我。 我有两个数字,我想将数据从第一个数字传递到第二个数字。 代码:

function pushbutton1_Callback(hObject, eventdata, handles)
    tryText=get(handles.text2,'String');
    open SecondPage.fig
guidata(hObject,handles);

如何在" tryText"中传递数据?并在第二个图中使用它?

我尝试使用以下格式传递数据:

function pushbutton1_Callback(hObject, eventdata, handles)
    tryText=get(handles.text2,'String');
    SecondPage(tryText)
guidata(hObject,handles);

但我没有运气检索它。 感谢

最新更新:

function varargout = first(varargin)
handles=struct;
datas=struct;
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @first_OpeningFcn, ...
                       'gui_OutputFcn',  @first_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

function first_OpeningFcn(hObject, eventdata, handles, varargin)
     handles.output = hObject;
guidata(hObject, handles);

function varargout = first_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;

function pushbutton1_Callback(hObject, eventdata, handles)
    struct.a=get(handles.edit1,'String');
        set(handles.text1,'String',struct.a);
        disp(struct);
    setappdata(0,'MyStruct',struct);
open second.fig

在第二时的意思是:

function varargout = second(varargin)
    getappdata(0,'MyStruct');
        gui_Singleton = 1;
        gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @second_OpeningFcn, ...
                       'gui_OutputFcn',  @second_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


function second_OpeningFcn(hObject, eventdata, handles, varargin)
    handles.output = hObject;
guidata(hObject, handles);

function varargout = second_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;


function pushbutton1_Callback(hObject, eventdata, handles)
disp(struct)
在第一个图中,disp(struct)的结果是正确的,而在第二个图中,它显示了没有字段的" 1x1结构数组。"
有人能突出我的错误吗?感谢

2 个答案:

答案 0 :(得分:0)

您需要使用句柄和数据结构来执行此操作。我将简单地指向an answer given in MATLAB Central,而不是向您展示如何在此处执行此操作,而是告诉您如何实现此目标。

答案 1 :(得分:0)

感谢您的回复,我终于设法使用以下代码。

代码:

  function varargout = second(varargin)

    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @second_OpeningFcn, ...
                   'gui_OutputFcn',  @second_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


function second_OpeningFcn(hObject, eventdata, handles, varargin)
    handles.output = hObject;
guidata(hObject, handles);

function varargout = second_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;


function pushbutton1_Callback(hObject, eventdata, handles)
    struct=getappdata(0,'MyStruct');
disp(struct)