필터 지우기
필터 지우기

Unable to run two function simultanously under App Designer

조회 수: 13 (최근 30일)
balandong
balandong 2020년 3월 10일
편집: Mohammad Sami 2020년 9월 21일
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