How to run the same timer callback function multiple times in parallel

9 views (last 30 days)
I am working on a matlab code that uses guide to run a timer which in turn run for one time only. The timer callback function counts 10 numbers starting from the number provided in the text field in the guide. when I enter a number then press start putton it would start counting. I would like to know how to entera second number while the code is running and make matlab counts 10 numbers for both values (The first and the second) in parallel.
let's say I entered 0 then I pressed the start button, then I immediately entered 10 and pressed the start button again. what happens now is that it counts only from 0 to 10. I would appreciate if you can share a way to make my code count from 0 to 10 and from 10 to 20 simultaneously in parrallel.
start button function :
function startbutton_Callback(hObject, eventdata, handles)
t=timer;
t.TimerFcn = @counter;
t.Period = 15;
t.ExecutionMode = 'fixedRate';
t.TasksToExecute = 1;
start(t);
stop (t);
delete (t);
Timer callback function:
function counter(~,~)
handles = guidata(counterFig);
num = str2double(get(handles.edit1,'String'));
for i = 0:10
disp (num);
num = num+1;
pause (1);
end
  6 Comments
Shubham Kashyap
Shubham Kashyap on 8 Apr 2020
You can use parallel toolbox for real parallel computation. or you call a function that will do something later and then give it a function as a parameter for what to do after it's done.
Walter Roberson
Walter Roberson on 8 Apr 2020
If you do use Parallel Toolbox, remember to take into consideration the following:
  • most hardware can only be accessed from one process at a time (the client is a process and the workers are each processes.) In some cases additional hardware access would be refused (already in use) and in other cases when you switch to a new process, the previous process loses access and has to re-request control. Cameras tend to lock, whereas GPU is an example of fighting for control
  • workers cannot access the graphics display (but they can graph and save the graphics to file)
  • output displayed in a worker might not be available until the parallel iteration finishes (when the results are ported back to the client)
  • communicating between worker and client takes time, so you should prefer to transfer less data. For example although it is feasible to dedicate one process to the camera to take the image, and send the image to another process to be processed through the alexnet, the data transfer of the image costs time that you would prefer to avoid if you can work it (by having alexnet run in the same process). Sometimes though transfering the data is the only feasible way to deal with hardware resources not being shared
  • consider using parfeval() to process the images

Sign in to comment.

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!