1

我想用 matlab 创建一个 gui 程序,但我想使用多个 gui。例如,我有主要的 gui 功能,我想从另一个带有编辑文本框的 gui 获取数据。在下面的示例中,我想将 p 变量返回到主 gui。

主界面:

function varargout = FoProgram(varargin)

    gui_Singleton = 0;
    gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FoProgram_OpeningFcn, ...
                   'gui_OutputFcn',  @FoProgram_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 FoProgram_OpeningFcn(hObject, eventdata, handles, varargin)

    handles.output = hObject;

    guidata(hObject, handles);

function varargout = FoProgram_OutputFcn(hObject, eventdata, handles) 
    varargout{1} = handles.output;


function labor_2_Callback(hObject, eventdata, handles) 

function fel1_Callback(hObject, eventdata, handles)
    cla reset;
    clc;
    clear all;

    n = guzu() %Here I call the second Gui function with edit textbox
    uiwait(gcf);

    x=linspace(-3*pi,3*pi,1000);
    y=sin(x);
    plot(x,y,'k','LineWidth',4)
    sz='ymcrgbkymcrgbkymcrgbkymcrgbk';
    hold on
    title('Sin(x) Taylor sora')
    %n = str2num(N);
    f=zeros(size(x));
    for i=1:n
        t=(-1)^(i-1)*x.^(2*i-1)/factorial(2*i-1);
        f=f+t;
        plot(x,f,sz(i),'LineWidth',2)
        axis([-10 10 -10 10])
        pause(1.5)
        hold on
        n=n+1;
    end



function exit_Callback(hObject, eventdata, handles)
    close

第二个图形用户界面

function varargout = guzu(varargin)

    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @guzu_OpeningFcn, ...
                       'gui_OutputFcn',  @guzu_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 guzu_OpeningFcn(hObject, eventdata, handles, varargin)
    handles.output = hObject;

    guidata(hObject, handles);

function varargout = guzu_OutputFcn(hObject, eventdata, handles) 
    varargout{1} = handles.output;

function edit1_Callback(hObject, eventdata, handles)

    p = str2double(get(hObject,'String')) %I want to return this 'p' to the main gui
    close

function edit1_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
4

1 回答 1

0

这种简单的用户输入使用该inputdlg功能很容易完成,无需创建单独的 GUI - 这是一个示例

inputTitle = 'Input Required';
inputPrompt = 'Enter a value for ''p'':';
userInput = inputdlg(inputTitle, inputPrompt);
if isempty(userInput)
    % User cancelled
    return;
else
    p = userInput{1}; % userInput is a cell array
    % Do something with p
end

您提供的示例可能是您的第二个 UI 的最终目标的一个非常小的版本,因此这可能不合适(尽管请注意该inputdlg函数能够执行一些更复杂的行为 - 请参阅文档)。如果您希望继续使用单独的 UI,则必须进行一些修改

%%% Add this line to the end of OpeningFcn
uiwait;


%%% Modify OutputFcn to have the following code:
varargout{1} = str2double(get(handles.edit1, 'String'));
% The figure can be deleted now
% NOTE: You have to change this to the name of your figure,
%       if it's not called figure1
delete(handles.figure1); 


%%% Add the CloseRequestFcn callback and put this code in it
if isequal(get(hObject, 'waitstatus'), 'waiting')
    % The GUI is still in UIWAIT, us UIRESUME
    uiresume(hObject);
else
    % The GUI is no longer waiting, just close it
    delete(hObject);
end

以上大部分内容都是直接从这里复制过来的。您还可以删除对uiwaitfrom的调用FoProgram

于 2013-05-19T10:23:34.840 回答