How to execute timer functions in MATLAB GUI in parallel?

I have designed a GUI using MATLAB GUIDE. I need to execute two timer functions in parallel. Let's say we have a timer and its function defined inside the GUI as follows:
handles.data_acq_tmr = timer(...
'ExecutionMode', 'fixedRate', ...
'Period', 0.2, ...
'Busymode','queue',...
'TimerFcn', {@data_read, hObject});
function data_read(~,~,hObject,~)
handles = guidata(hObject);
% do some processing here
guidata(hObject,handles);
and the second timer as follows:
handles.dataproc_tmr = timer(...
'ExecutionMode', 'singleShot', ...
'StartDelay', 0,...
'Busymode','queue',...
'TimerFcn', {@data_proc, hObject});
function data_proc(~,~,hObject,~)
handles = guidata(hObject);
% do some other processing here
guidata(hObject,handles);
Is there any way to do it in MATLAB for instance using Parallel Computing Toolbox?

댓글 수: 6

RZM - you should be able to start both timers and so have both run concurrently without the need for the Parallel Processing Toolbox (which I'm not sure would help in this case). Are you observing an error when you try to do this? Why is your second timer a "single shot"?
Geoff, Parallel helps run expensive things in the background asynchronously. While a timer appears to be asynchronous, it still ties up the main thread with heavy compute.
Thanks for the explanation, Sean. I should probably get that toolbox!
Dear Geoff, I actually use more timers but here mentioned two for simplicity. Some of the timer functions are needed to be executed after the execution of another timer like a sequence, that is why I use 'singleShot'. I experienced that the some of the timer functions fail to execute when they are running concurrently. For this reason I thought I could use Parallel Computing toolbox, but still it seems so complex how to implement parallel processing in the structure of MATLAB GUI.
RZM - what are the callbacks for your timers doing? Are they performing complex and/or expensive operations?
RZM
RZM 2018년 8월 17일
편집: RZM 2018년 8월 17일
One reads the data transferred from another computer, and the other one does some processing on a sequence of recorded data including filtering, and some expensive operations on multiple variables. There are some other timers to control the presentation of some visual stimuli on the screen.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sean de Wolski
Sean de Wolski 2018년 8월 15일

1 개 추천

Use parfeval in the parallel computing toolbox to asynchronously run the computationally expensive algorithmic (not gui updates etc.) part in the background. Use a timer to poll the job for when its state is "finished" for when it's done to update the user interface.

댓글 수: 5

I am new to parallel Computing Toolbox. Would you mind explain more? How could you use parfeval into the structure of the timers and their functions?
RZM
RZM 2018년 8월 15일
편집: RZM 2018년 8월 15일
You mean in this example I define the timer like this?
paralelledF = parfeval(@data_read,hObject)
handles.data_acq_tmr = timer(...
'ExecutionMode', 'fixedRate', ...
'Period', 0.2, ...
'Busymode','queue',...
'TimerFcn', paralelledF);
function data_read(~,~,hObject,~)
handles = guidata(hObject);
% do some processing here
guidata(hObject,handles);
Hi RZM,
Attached is an example that does it for running unit tests in the background and reloading them in a browser. The general pattern will be the same though you won't be using a browser or unit tests.
You won't want to pass hObject to the parfeval function. It should be purely compute and not dependent on any graphics components.
Thank you Sean, I still cannot figure out what to do.
@Claudia-Elena Ilie's answer moved here.
Hello, @RZM,
By any chance, you understood how to rezolve your problem? I have something similar :)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

제품

릴리스

R2015a

질문:

RZM
2018년 8월 15일

댓글:

2022년 7월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by