MATLAB APP Designer多窗口间数据传递。

一、思路

网上说的太复杂了,其实关键就两步:

1.增加子界面的startupFcn的输入参数,将主界面的app指针加进来。

2.主界面调用子界面的时候,调用子界面的构造函数时,把自己的指针加进去。

3. 别用matlab2016a!别用matlab2016a!别用matlab2016a!很多属性都改不了,巨坑。

二、例程

举个例程,实现如下功能:

1. 点击主界面的button,随机生成一个随机数,同时将随机数传给子界面。同时打开子界面。

2. 点击子界面的button,画出一条曲线,同时将曲线同步到主界面。

三、实现

1.创建主界面(名称为App1)。界面及各控件名称如下。

2.创建子界面【sub_figure】。

 3. 修改子界面【sub_figure】:

① 创建一个属性【app_parent】用来存储主界面的指针。

② 修改startupFcn,将主界面的指针传进来。

③ 将主界面控件的【app_parent.nEdit_1】的值提取出来给子界面的控件【app.nEdit_1】

 

4. 编辑主界面的【btn_1】的回调函数,实现:

① 生成随机数,并将随机数赋值给控件【nEdit_1】。

② 调用子界面的构造函数打开子界面。

function btn_1ButtonPushed(app, event)
    app.nEdit_1.Value=rand();   % 生成随机数
    % 调用子界面,同时将主界面句柄(app)作为子界面初始化的参数传入
    app.sub_app = sub_figure(app);
end

 5.  编辑子界面按钮的回调函数,在子界面画曲线,同时通过指针的方式,修改主界面的坐标轴。

function btn_1ButtonPushed(app, event)
    x = 0:0.01:2*pi;
    y = sin(x) + 0.1 * rand(size(x));
    plot(app.ax_1, x, y);
    % 将曲线画到主界面。
    plot(app.app_parent.ax_1, x, y);

end

四、运行效果

 五、完整代码

懒得传资源了,自己copy代码吧。注意,我用的版本是matlab2020a

1. 注意,copy之后文件名一定要跟我代码一致。主界面文件名为【App1.mlapp】,子界面文件名为【sub_figure.mlapp】

主界面:

classdef App1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        mainFigure             matlab.ui.Figure
        btn_1                  matlab.ui.control.Button
        LabelNumericEditField  matlab.ui.control.Label
        nEdit_1                matlab.ui.control.NumericEditField
        ax_1                   matlab.ui.control.UIAxes
    end


    properties (Access = private)
        sub_app; % Description
    end


    % Callbacks that handle component events
    methods (Access = private)

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

        % Button pushed function: btn_1
        function btn_1ButtonPushed(app, event)
            app.nEdit_1.Value=rand();   % 生成随机数
            % 调用子界面,同时将主界面句柄(app)和随机数作为子界面初始化的参数传入
            app.sub_app = sub_figure(app);
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create mainFigure and hide until all components are created
            app.mainFigure = uifigure('Visible', 'off');
            app.mainFigure.Position = [101 101 640 480];
            app.mainFigure.Name = '主界面';

            % Create btn_1
            app.btn_1 = uibutton(app.mainFigure, 'push');
            app.btn_1.ButtonPushedFcn = createCallbackFcn(app, @btn_1ButtonPushed, true);
            app.btn_1.Position = [264 88 178 65];
            app.btn_1.Text = 'button';

            % Create LabelNumericEditField
            app.LabelNumericEditField = uilabel(app.mainFigure);
            app.LabelNumericEditField.HorizontalAlignment = 'right';
            app.LabelNumericEditField.VerticalAlignment = 'top';
            app.LabelNumericEditField.Position = [425 387 49 15];
            app.LabelNumericEditField.Text = '编辑字段';

            % Create nEdit_1
            app.nEdit_1 = uieditfield(app.mainFigure, 'numeric');
            app.nEdit_1.Position = [489 383 100 22];

            % Create ax_1
            app.ax_1 = uiaxes(app.mainFigure);
            title(app.ax_1, 'Title')
            xlabel(app.ax_1, 'X')
            ylabel(app.ax_1, 'Y')
            app.ax_1.GridAlpha = 0.15;
            app.ax_1.MinorGridAlpha = 0.25;
            app.ax_1.Position = [1 171 400 300];

            % Show the figure after all components are created
            app.mainFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = App1

            % Create UIFigure and components
            createComponents(app)

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

            % 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.mainFigure)
        end
    end
end 

子界面:

classdef sub_figure < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        subFigure       matlab.ui.Figure
        EditFieldLabel  matlab.ui.control.Label
        nEdit_1         matlab.ui.control.NumericEditField
        btn_1           matlab.ui.control.Button
        ax_1            matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        app_parent; % Description
        x = 0;
    end


    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app, app_parent)
            app.app_parent = app_parent;
            app.nEdit_1.Value = app_parent.nEdit_1.Value;        
        end

        % Button pushed function: btn_1
        function btn_1ButtonPushed(app, event)
            x = 0:0.01:2*pi;
            y = sin(x) + 0.1 * rand(size(x));
            plot(app.ax_1, x, y);
            % 将曲线画到主界面。
            plot(app.app_parent.ax_1, x, y);
            
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create subFigure and hide until all components are created
            app.subFigure = uifigure('Visible', 'off');
            app.subFigure.Position = [101 101 640 480];
            app.subFigure.Name = '子界面';

            % Create EditFieldLabel
            app.EditFieldLabel = uilabel(app.subFigure);
            app.EditFieldLabel.HorizontalAlignment = 'right';
            app.EditFieldLabel.Position = [126 323 55 22];
            app.EditFieldLabel.Text = 'Edit Field';

            % Create nEdit_1
            app.nEdit_1 = uieditfield(app.subFigure, 'numeric');
            app.nEdit_1.Position = [196 319 67 29];

            % Create btn_1
            app.btn_1 = uibutton(app.subFigure, 'push');
            app.btn_1.ButtonPushedFcn = createCallbackFcn(app, @btn_1ButtonPushed, true);
            app.btn_1.Position = [292 189 100 22];

            % Create ax_1
            app.ax_1 = uiaxes(app.subFigure);
            title(app.ax_1, 'Title')
            xlabel(app.ax_1, 'X')
            ylabel(app.ax_1, 'Y')
            zlabel(app.ax_1, 'Z')
            app.ax_1.Position = [292 256 300 185];

            % Show the figure after all components are created
            app.subFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = sub_figure(varargin)

            % Create UIFigure and components
            createComponents(app)

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

            % Execute the startup function
            runStartupFcn(app, @(app)startupFcn(app, varargin{:}))

            if nargout == 0
                clear app
            end
        end

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

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

  • 6
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
MATLAB App Designer 中,可以通过以下步骤在不同窗口之传递数据: 1. 在需要传递数据的窗口中,创建一个公共属性(例如,在窗口中添加一个文本框)。 2. 在另一个窗口的回调函数中,使用 `app` 对象来获取第一个窗口的实例,并获取公共属性的值。 下面是一个简单的示例代码: 在第一个窗口中,添加一个文本框,将其 Tag 属性设置为 "mytextbox": ```matlab classdef App1 < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure mytextbox matlab.ui.control.TextBox end % Code that executes after component creation methods (Access = private) % Button pushed function: Button function ButtonPushed(app, event) app.mytextbox.Value = 'Hello from App1'; end end % Callbacks that handle component events methods (Access = private) % Value changed function: mytextbox function mytextboxValueChanged(app, event) value = app.mytextbox.Value; app.mytextbox.Value = 'You cannot edit this textbox!'; 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 = 'App1'; % Create mytextbox app.mytextbox = uitextbox(app.UIFigure); app.mytextbox.Tag = 'mytextbox'; app.mytextbox.Editable = 'off'; app.mytextbox.Position = [100 100 200 22]; app.mytextbox.Value = 'You can edit this textbox!'; % Create Button app.Button = uibutton(app.UIFigure, 'push'); app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true); app.Button.Position = [100 200 100 22]; app.Button.Text = 'Click me!'; end end % App creation and deletion methods (Access = public) % Construct app function app = App1 % Create UIFigure and components createComponents(app) end end end ``` 在第二个窗口中,添加一个按钮,并在按钮的回调函数中获取第一个窗口的实例,并获取公共属性的值: ```matlab classdef App2 < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure Button matlab.ui.control.Button end % Code that executes after component creation methods (Access = private) % Button pushed function: Button function ButtonPushed(app, event) % Get instance of App1 app1 = findall(groot, 'Tag', 'App1'); % Get value of mytextbox in App1 value = app1.mytextbox.Value; % Show value in a message box uialert(app.UIFigure, value, 'Value'); 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 = 'App2'; % Create Button app.Button = uibutton(app.UIFigure, 'push'); app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true); app.Button.Position = [100 100 100 22]; app.Button.Text = 'Get Value'; end end % App creation and deletion methods (Access = public) % Construct app function app = App2 % Create UIFigure and components createComponents(app) end end end ``` 在这个例子中,当点击 App2 窗口中的按钮时,会获取 App1 窗口中的 mytextbox 的值,并在一个消息框中显示出来。 注意,为了获取第一个窗口的实例,我们使用了 `findall(groot, 'Tag', 'App1')`,其中 'App1' 是第一个窗口的名称。在这个例子中,我们假设第一个窗口的名称为 'App1'。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值