Unable to run two function simultanously under App Designer

5 次查看(过去 30 天)
Dear Coder,
Objective
The objective is to run two functions simultaneously using app that design using App designer.
In general, there are 3 main blocks
1) App1. The apps contains 1 state button (e.g., STOP BUTTON) and 1 button (TASK).
2) Function FirstTask. Under the hood, datetime is sampled at every iteration.
3) Function SecondTask. Similarly, under the hood, datetime is sampled at every iteration
** For reproduciabilty, we simplify the example of sampling datatime.
The procedure are as follow;
Function FirstTask is execute at the beginning/startup of App1. Wheras, Function SecondTask only executed after the Button TASK being pressed.
Both Function FirstTask and Function SecondTask terminated simultaneously after STOP BUTTON being pressed.
Observations
There are three observations made;
1) Function FirstTask is execute as intended at the beginning/startup of App1.
2) Function SecondTask is execute as intended after the Button TASK being pressed.
3) The Function FirstTask is put to halt after Button TASK being pressed despite being independent of the Button TASK.
My question is, how to tackle the observation No 3. Because, we required both Function FirstTask and Function SecondTask to run simultaneously.
The code to reproduce the above problem are
1) Code at the app1.
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
FirstTask(app)
end
% Button pushed function: RunSecondTaskButton
function RunSecondTaskButtonPushed(app, event)
SecondTask(app)
end
end
2) Function FirstTask
function FirstTask(Gui)
initVar=1;
MaximumData=1000; % Maximum before we append further
FirstData=NaT(MaximumData,1); % Prelocate
while Gui.StopButton.Value==0 % Loop while button stop no click
FirstData(initVar)=datetime('now','Format','HH:mm:ss.SSS'); % add the time vector duration for each day
initVar=initVar+1;
pause(1)
end
end
3) Function SecondTask
function SecondTask(Gui)
initVar=1;
MaximumData=1000; % Maximum before we append further
SecondData=NaT(MaximumData,1); % Prelocate
while Gui.StopButton.Value==0 % Loop while button stop no click
SecondData(initVar)=datetime('now','Format','HH:mm:ss.SSS'); % add the time vector duration for each day
initVar=initVar+1;
pause(1)
end
end
The full code is attach in this thread.
Really appreciate for any advice on this matter.
  1 个评论
Peter Hou
Peter Hou 2020-9-17
Hi balandong, have you found a solution for this? I am having a similar problem as yours. if you had solved this problem, can you please advise how to do it? Thank you

请先登录,再进行评论。

回答(1 个)

Mohammad Sami
Mohammad Sami 2020-9-18
MATLAB is single threaded, if you wish to run tasks in parallel, you have to use parpool from parallel computing toolbox.
  2 个评论
Peter Hou
Peter Hou 2020-9-21
Do you have any example links which will help us to briefly know how to do this? Thank you for your help.
I tried to use parpool but I dont know how to assign my task 1 and 2 to the coding.
Mohammad Sami
Mohammad Sami 2020-9-21
编辑:Mohammad Sami 2020-9-21
I think you can use parfeval function to run your task. It executes the task asynchronously and gives you a future object. You can set a callback to be called on task completion using aftereach function.
The reference documentation is here. https://www.mathworks.com/help/parallel-computing/parallel.pool.parfeval.html

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by