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
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: Matlab App Designer中的定时器可以用于定期执行某些操作,例如更新UI组件或执行计算任务。要使用定时器,可以按照以下步骤操作: 1. 在App Designer中打开“App Settings”窗口,选择“Code View”选项卡。 2. 在代码编辑器中添加一个定时器对象,例如: app.Timer = timer('ExecutionMode', 'fixedRate', 'Period', 1, 'TimerFcn', @app.updateUI); 这里创建了一个定时器对象,它的执行模式为“fixedRate”,每隔1秒钟执行一次,执行的函数为“updateUI”。 3. 在App Designer中添加一个按钮或其他UI组件,用于启动或停止定时器。 4. 在按钮的回调函数中添加以下代码,以启动或停止定时器: if strcmp(app.Timer.Running, 'off') start(app.Timer); else stop(app.Timer); end 这里使用了定时器对象的“Running”属性来判断定时器当前是否正在运行,如果没有运行,则启动定时器,否则停止定时器。 5. 在App Designer中添加一个更新UI的函数,例如: function updateUI(app, ~) % 更新UI组件的代码 end 这里的“~”表示定时器对象本身不需要传递任何参数,只需要传递App Designer对象即可。 通过以上步骤,就可以在Matlab App Designer中使用定时器实现定期更新UI组件或执行计算任务。 ### 回答2: MATLAB App Designer 是用于创建 MATLAB 应用程序的交互式工具箱。此工具箱允许开发者使用 MATLAB 语言创建自定义窗体、面板和控件来定义应用程序的外观和行为。其中一个常用的控件是定时器。下面将详细介绍 MATLAB App Designer 中的定时器。 App Designer 中的定时器控件可以用于周期性地触发某个事件,例如更新界面、检查数据等。在 App Designer 中添加定时器有两种方法: 1. 在 App Designer 中单击“工具箱”的“基本工具”选项卡下的“定时器”来添加一个新的定时器。 2. 在 MATLAB 编辑器中编写处理定时器事件的回调函数后,在 App Designer 中单击“Code View”按钮,然后在 App Designer 中拖拽新的定时器控件并为其指定相应的回调函数。 无论你使用哪种方法,在定时器的“Properties”选项卡下都可以设置定时器的工作方式。这些属性包括: 1. “延迟时”(Delay):指定定时器开始启动之前需要等待的时。 2. “周期时”(Period):指定定时器在一次启动和下一次启动之需要等待的时。 3. “任务队列”(TasksToExecute):指定定时器应该执行多少次。如果此属性设为“inf”(无穷大),则定时器将无限执行。 在回调函数中,可以使用“timer”对象的属性和方法来控制定时器的行为。例如,可以使用“timer”对象的“Stop”方法停止定时器的执行,以使它不再触发回调函数。 总之,定时器是一个非常有用的控件,可助于开发者实现复杂的周期性任务。在 App Designer 中,可以通过添加定时器控件并设置其属性来创建定时触发事件的应用程序。 ### 回答3: Matlab App Designer定时器是一种非常有用的工具,可以帮助您确保您的应用程序在预定时内执行所需的操作。通过设置定时器,您可以自动执行代码,而无需等待用户交互或其他操作触发代码。 Matlab App Designer定时器是基于时钟对象构建的,您可以通过设置和处理时钟对象的各种属性来实现定时器的不同功能。例如,您可以设置时钟对象的周期时和执行函数,以确保您的应用程序在规定的时隔内执行代码。 当您使用Matlab App Designer定时器时,您需要理解定时器的一些关键方面。首先,您需要选择一个适当的时钟对象类型,以确保您的应用程序能够实现所需的功能。时钟对象类型通常包括:once、repeat、startDelay等。 其次,您需要设置定时器的周期时,以确保代码可以按照所需的时隔执行。周期时可以是任何值,但应根据您的应用程序的需求进行调整,确保您的代码可以在正确的时执行。 最后,您需要设置执行函数,以确保您的代码可以在定时器周期内执行所需的操作。执行函数应包括所有必要的代码,以确保您的应用程序可以实现所需的功能。 总之,Matlab App Designer定时器是一种非常有用的工具,可以帮助您管理和控制应用程序中的操作。通过设置和处理定时器的各种属性,您可以确保您的应用程序在正确的时内执行必要的操作,从而提高应用程序的效率和功能性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值