How to run a Matlab script asynchronously from a Matlab app?

17 views (last 30 days)
I currently have an app that has a button that is supposed to start and stop another matlab script. The other Matlab script is supposed to run until the button in the app is pushed again.
The app's logic looks roughly like this:
function StartDataCollectionButtonPushed(app, event)
% app.experiment_running is a public variable
if(app.experiment_running == false)
% Change state of app to indicate experiment is running
app.experiment_running = true;
app.StartDataCollectionButton.BackgroundColor = [1 0 0];
app.StartDataCollectionButton.Text = "Stop Data Collection";
% Placeholder value, assume this is a valid file path.
run("Matlab_File.m");
else
% Change state of app to indicate no experiment is running
app.experiment_running = false;
app.StartDataCollectionButton.BackgroundColor = [0 1 0];
app.StartDataCollectionButton.Text = "Start Data Collection";
% Need way of quitting Matlab_File.m
end
else
And the external Matlab_File.m looks like this:
z = 0;
while app.experiment_running
% This counter is here so that the script will eventually quit on its own if I cannot stop it
z = z+1
if z >= 1e5
return
end
end
The problem is that run("Matlab_File.m") is a blocking call, so even if I click on the "StartDataCollectionButton" as Matlab_File.m is executing, experiment_running isn't set to false until Matlab_File.m returns on its own. I'd like to be able to change that flag in the middle of Matlab_File's execution and have it exit the loop and return.

Accepted Answer

Jon
Jon on 18 Apr 2022
One way would be to use a MATLAB timer and make your script that you want to execute asynchonously be the timer's callback function. Note that if this script is to execute repeatedly you can set up the timer for periodic execution (rather than using a loop)
Please see this link for general info on setting up timers and timer functions: https://www.mathworks.com/help/matlab/matlab_prog/use-a-matlab-timer-object.html
Timers have a stop function that could be called when you push the button to stop the test.
If there is some cleanup that must be done when the timer stops you can also provide the timer with a stop callback function to perform those steps.
  5 Comments
Jan Bartels
Jan Bartels on 20 Apr 2022
Edited: Jan Bartels on 20 Apr 2022
For posterity, I was able to do what I wanted with the following setup. In the app I have the following code:
function StartDataCollectionButtonPushed(app, event)
% app.experiment_running is a public variable
if(app.experiment_running == false)
% Change state of app to indicate experiment is running
app.experiment_running = true;
app.StartDataCollectionButton.BackgroundColor = [1 0 0];
app.StartDataCollectionButton.Text = "Stop Data Collection";
% This block uses a timer to run the Data
% Collection Script (DCS) asynchronously in the
% background.
s = strcat("run('","Matlab_File.m","')");
app.t = timer('TimerFcn','disp(''.'')','StartFcn',s,'StopFcn','','StartDelay',0);
% It then pushes a new variable called
% 'experimentRunning' into the base workspace that
% the DCS can access.
% The DCS needs to collect data while
% experimentRunning is true, and save data & return
% when 'experimentRunning' goes to 0.
assignin('base','experimentRunning',1);
% This line calls the 'StartFcn' function of the
% timer, which is declared as s a few lines above.
start(app.t);
else
% Change state of app to indicate no experiment is running
app.experiment_running = false;
app.StartDataCollectionButton.BackgroundColor = [0 1 0];
app.StartDataCollectionButton.Text = "Start Data Collection";
% Quit the Matlab script by setting 'experimentRunning' to
% 0. The expectation is that this will cause the script to
% quit collecting and store its data.
assignin('base','experimentRunning',0);
stop(app.t); % Stop and close the timer.
delete(app.t);
end
end
And the matlab script looks like this:
z = 0;
while experimentRunning
z = z + 1;
disp(z)
% Counter only used for development, to ensure script will eventually
% return if experimentRunning isn't being set correctly
if z >= 8e4
return
end
% This pause is necessary. Without it the script never updates experimentRunning
pause(0.0001)
end
% Add code to save data here
Jon
Jon on 20 Apr 2022
That's good, I'm glad you were able to get it working using the timer

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 20 Apr 2022
Edited: Bruno Luong on 20 Apr 2022
@Jan Bartels "The problem is that run("Matlab_File.m") is a blocking call, so even if I click on the "StartDataCollectionButton" as Matlab_File.m is executing, experiment_running isn't set to false until Matlab_File.m returns on its own."
I have implemented the same pattern and I don't think it's true, Callback is even driven. You have to make sure the properties 'Interruptible' is set to 'on' and 'BusyAction' to 'queue'. When you click it will change, you might add 'redraw' statement at the end of the callback and before the check in your script so that the event is correctly broadcasted and arrived.
You might add some code to protect with user click twice and the script have some latency to capture the stop event or such.
  2 Comments
Jan Bartels
Jan Bartels on 20 Apr 2022
I will have to look into this. Looking at the app, the button has been set to be interruptible and busy action has been set to queue.
Currently the behavior is that if I click the button while the blocking script is running it will wait to change the color until the script has finished. Clicking the button multiple times will cause the app to wait until the script has finished and then relaunch it so the queue behavior definitely seems to be there.
Maybe the issue has been that the script didn't have a pause statement. I will test this.
Bruno Luong
Bruno Luong on 20 Apr 2022
pause(x) will do a redraw
redraw is like pause with no waittime.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!