How to Allow a Loop to Continue Running While Gui is Open Matlab

Yes it's possible...
It's not the best programming pattern, but it's very convenient approach for small software projects.

I recommend you to use MATLAB App Designer.
App Designer uses OOP programming model, that makes it simpler to pass data (without using global variables, and without complicated solutions that are used with GUIDE).

Here is a sample instructions:

  1. Start App Designer - execute appdesigner from command line.
  2. Add two buttons, and two labels (add anything else later).
  3. Add callbacks: for start button set flag to true and in stop button to false.
    The syntax is app.is_sending = true (when is_sending is a property of app).
  4. Change from Design View to Code View.
  5. Add private properties: loop_counter = 0;, sending_counter = 0;, is_sending = false;.
  6. Add callback - select startupFcn callback function.
  7. Put your while loop in the startupFcn callback.
    Use while isvalid(app)
  8. Place your while loop functionality inside the while loop.
  9. Place your sending code with if (app.is_sending)...
  10. Very important: at the end of the loop call pause function.
    You must execute pause or drawnow to allow callbacks to be responsive.

When using App Designer, some code is generated automatically, and cannot be modified in Code View.
The following code sample includes both generated code, and customized code:

          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                  

You can copy and paste the entire code into an m file, to see how it works.

The code sample is not sending any data (and not related to Nucleo board), it's just advancing counters.

Here is how the sample application interface looks like:
enter image description here

mullinshather.blogspot.com

Source: https://stackoverflow.com/questions/59095112/operating-on-while-loop-outside-the-loop-matlab

0 Response to "How to Allow a Loop to Continue Running While Gui is Open Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel