【问题标题】:Operating on while loop outside the loop matlab在循环matlab外的while循环上操作
【发布时间】:2020-03-24 11:31:19
【问题描述】:

我的 Nucleo 有问题。

我正在使用 Matlab 与我的 Nucleo 板配合。

我想以编程方式构建带有按钮、图形等的 GUI。我将把整个功能放在 while 循环中。现在是我的问题。

是否有可能将整个代码放入while循环并通过循环外的回调函数对其进行操作? 例如:我的循环我想在 btn1 按下时向 Nucleo 发送一些数据,我想在 btn2 按下时停止它(当然是 btns 的 if 语句)。是否有可能通过更改按钮值或类似的东西(标志等)来做到这一点?

我不想使用全局变量。

【问题讨论】:

  • 为什么要使用while循环?阅读有关如何编写 MATLAB GUI 的信息。这一切都适用于回调,您不需要循环。
  • 我需要 while 循环,因为稍后,板将接收来自 PPG 传感器的实时数据并将其发送到 matlab。我想执行避免全局变量的所有功能

标签: matlab user-interface while-loop


【解决方案1】:

是的,有可能……
这不是最好的编程模式,但对于小型软件项目来说它是一种非常方便的方法。

我建议你使用MATLAB App Designer
App Designer 使用 OOP 编程模型,这使得传递数据变得更简单(不使用全局变量,也没有与 GUIDE 一起使用的复杂解决方案)。

这里是一个示例说明:

  1. 启动 App Designer - 从命令行执行 appdesigner
  2. 添加两个按钮和两个标签(稍后添加其他内容)。
  3. 添加回调:开始按钮设置标志为true,停止按钮设置为false
    语法为app.is_sending = true(当is_sendingapp 的属性时)。
  4. 设计视图更改为代码视图
  5. 添加私有属性:loop_counter = 0;sending_counter = 0;is_sending = false;
  6. 添加回调 - 选择 startupFcn 回调函数。
  7. 将您的 while 循环放入 startupFcn 回调中。
    使用while isvalid(app)
  8. 将您的while 循环功能放在while 循环中。
  9. 将您的发送代码放在if (app.is_sending)...
  10. 非常重要:在循环结束时调用pause 函数。
    您必须执行 pausedrawnow 以允许回调响应。

使用 App Designer 时,部分代码会自动生成,无法在代码视图中修改。
以下代码示例包括生成的代码和自定义代码:

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure            matlab.ui.Figure
        StartSendingButton  matlab.ui.control.Button
        StopSendingButton   matlab.ui.control.Button
        Sending0Label       matlab.ui.control.Label
        LoopCounter0Label   matlab.ui.control.Label
    end


    properties (Access = private)
        loop_counter = 0 % Count while loop iteration
        sending_counter = 0; %Count only while "sending data"
        is_sending = false;  %Flag: if value is true, assume "sending"
    end


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

        % Code that executes after component creation
        function startupFcn(app)
            %While loop: loop until app is not valid.
            while isvalid(app)
                %Place your while loop functionality here:
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                app.loop_counter = app.loop_counter + 1; %Increase loop counter
                app.LoopCounter0Label.Text = ['Loop Counter: ', num2str(app.loop_counter)]; %Update loop coouner text lable.
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

                %Place your sending code here:
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                if (app.is_sending)
                    app.sending_counter = app.sending_counter + 1; %Increase sending counter
                    app.Sending0Label.Text = ['Sending: ', num2str(app.sending_counter)]; %Update sending coouner text lable.
                end
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                

                %%%% Important %%%%
                %Short pause must be used in order to allow buttons callbacks to be responsive (you can also call drawnow).
                pause(0.01)
                %%%% Important %%%%
            end
        end

        % Button pushed function: StartSendingButton
        function StartSendingButtonPushed(app, event)
            app.is_sending = true; %Set flag to true when button is pressed.
        end

        % Button pushed function: StopSendingButton
        function StopSendingButtonPushed(app, event)
            app.is_sending = false; %Set flag to false when button is pressed.
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 440 304];
            app.UIFigure.Name = 'UI Figure';

            % Create StartSendingButton
            app.StartSendingButton = uibutton(app.UIFigure, 'push');
            app.StartSendingButton.ButtonPushedFcn = createCallbackFcn(app, @StartSendingButtonPushed, true);
            app.StartSendingButton.BackgroundColor = [0.3922 0.8314 0.0745];
            app.StartSendingButton.FontSize = 16;
            app.StartSendingButton.Position = [33 197 130 49];
            app.StartSendingButton.Text = 'Start Sending';

            % Create StopSendingButton
            app.StopSendingButton = uibutton(app.UIFigure, 'push');
            app.StopSendingButton.ButtonPushedFcn = createCallbackFcn(app, @StopSendingButtonPushed, true);
            app.StopSendingButton.BackgroundColor = [0.851 0.3255 0.098];
            app.StopSendingButton.FontSize = 16;
            app.StopSendingButton.Position = [33 129 130 49];
            app.StopSendingButton.Text = 'Stop Sending';

            % Create Sending0Label
            app.Sending0Label = uilabel(app.UIFigure);
            app.Sending0Label.FontSize = 16;
            app.Sending0Label.Position = [229 203 151 37];
            app.Sending0Label.Text = 'Sending: 0';

            % Create LoopCounter0Label
            app.LoopCounter0Label = uilabel(app.UIFigure);
            app.LoopCounter0Label.FontSize = 16;
            app.LoopCounter0Label.Position = [37 44 193 37];
            app.LoopCounter0Label.Text = 'Loop Counter: 0';

            % Show the figure after all components are created
            app.UIFigure.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.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

您可以将整个代码复制并粘贴到m 文件中,看看它是如何工作的。

代码示例不发送任何数据(与 Nucleo 板无关),它只是推进计数器。

示例应用程序界面如下所示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2016-11-27
    相关资源
    最近更新 更多