Unable to run two function simultanously under App Designer

16 visualizaciones (últimos 30 días)
balandong
balandong el 10 de Mzo. de 2020
Editada: Mohammad Sami el 21 de Sept. de 2020
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 comentario
Peter Hou
Peter Hou el 17 de Sept. de 2020
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

Iniciar sesión para comentar.

Respuestas (1)

Mohammad Sami
Mohammad Sami el 18 de Sept. de 2020
MATLAB is single threaded, if you wish to run tasks in parallel, you have to use parpool from parallel computing toolbox.
  2 comentarios
Peter Hou
Peter Hou el 21 de Sept. de 2020
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 el 21 de Sept. de 2020
Editada: Mohammad Sami el 21 de Sept. de 2020
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

Iniciar sesión para comentar.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by