Matlab 将数据传递给另一个 GUI

Matlab passing data to another GUI

我是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 struct array with no fields”。
有人可以强调我的错误在哪里吗?谢谢

您需要使用句柄和数据结构来做到这一点。我不会在这里向您展示如何做到这一点,而只是将您指向 an answer given in MATLAB Central,它告诉您如何实现这一目标。

感谢您的回复,我终于设法让它与以下代码一起工作。

代码:

  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)