как передавать данные из одного приложения в другое в Matlab GUI (с помощью дизайнера приложений) - PullRequest
0 голосов
/ 18 октября 2019

Я создаю графический интерфейс, в котором я буду печатать что-то в первом приложении и хочу, чтобы оно появилось во втором приложении. эти два приложения связаны друг с другом, т.е. приложение 1 - это первая страница графического интерфейса, а приложение 2 - вторая страница.

1 Ответ

1 голос
/ 18 октября 2019

Это просто, когда App1 держит дескриптор для App2.

App1:

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

Выполнение App2 из App1:

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

Установка метки в App2 из App1 при нажатии кнопки:

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

Полный код приложения 1 (образец):

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

Полный код приложения 2 (образец):

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...