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 个评论
Geoff Hayes
2018-8-15
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"?
Sean de Wolski
2018-8-15
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.
Geoff Hayes
2018-8-15
Thanks for the explanation, Sean. I should probably get that toolbox!
RZM
2018-8-15
Geoff Hayes
2018-8-17
RZM - what are the callbacks for your timers doing? Are they performing complex and/or expensive operations?
回答(1 个)
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 个评论
RZM
2018-8-15
Sean de Wolski
2018-8-15
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.
RZM
2018-8-17
Geoff Hayes
2022-7-1
@Claudia-Elena Ilie's answer moved here.
By any chance, you understood how to rezolve your problem? I have something similar :)
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!