简体   繁体   English

如何在 matlab gui 中将数据从一个应用程序传递到另一个应用程序(使用应用程序设计器)

[英]how to pass data from one app to another in matlab gui (using app designer)

I am designing a Gui whereby I will type something on the first app and I want it to appear on the second app.我正在设计一个 Gui,我将在第一个应用程序上输入一些内容,并希望它出现在第二个应用程序上。 these two apps are linked to each other ie app 1 is the first page of GUI and app 2 is the second page.这两个应用程序相互链接,即应用程序 1 是 GUI 的第一页,应用程序 2 是第二页。

It's simple when App1 holds a handle to App2.当 App1 持有 App2 的句柄时,这很简单。

App1:应用程序1:

properties (Access = private)
    hApp2 % Handle to app2
end

Executing App2 from App1:从 App1 执行 App2:

% Code that executes after component creation
function startupFcn(app)
    app.hApp2 = App2;
end

Setting a label in App2 from App1 when button is pressed:按下按钮时,从 App1 在 App2 中设置 label:

% Button button pushed function
function ButtonButtonPushed(app)
    app.hApp2.Label.Text = 'Button Pressed';
end

App1 complete code (sample): App1完整代码(示例):

classdef App1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure matlab.ui.Figure         % UI Figure
        Button   matlab.ui.control.Button % Button
    end


    properties (Access = private)
        hApp2 % Handle to app2
    end


    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            app.hApp2 = App2;
        end

        % Button button pushed function
        function ButtonButtonPushed(app)
            app.hApp2.Label.Text = 'Button Pressed';
        end
    end

    % App initialization and construction
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure
            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'UI Figure';
            setAutoResize(app, app.UIFigure, true)

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonButtonPushed);
            app.Button.Position = [231 261 153 61];
        end
    end

    methods (Access = public)

        % Construct app
        function app = App1()

            % Create and configure components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

App2 complete code (sample): App2完整代码(示例):

classdef App2 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure matlab.ui.Figure        % UI Figure
        Label    matlab.ui.control.Label % Label
    end

    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)

        end
    end

    % App initialization and construction
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure
            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'UI Figure';
            setAutoResize(app, app.UIFigure, true)

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.Position = [236 338 63 21];
        end
    end

    methods (Access = public)

        % Construct app
        function app = App2()

            % Create and configure components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM