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

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

Only one timer callback can be active at a time: all of the other timer callbacks have to wait.
However you could redefine your timer code into something that executes every 1 second (and did not have a pause) and use different timers.
But more often in cases like this it is better to have a single timer ever 1 second that has a queue of tasks that it runs through.
Dear Walter Roberson, Thanks for the answer. Infact I am working on an image processing application so that the timer callback function caputres an image and uses Alexnet to detect the waste in the image but I have made this counter callback function for simplicity. This why I do not expect the timer callback function to execute every 1 second. The application that I'm working on should support multiple cameras and each camera should capture an image and detect the waste perodically based on time determined by the user. May I know if there is any clue how may I get this accomplished
Dear Ebrahim,
could you solve your issue back then?
Currently I am having a similar issue with parallel events which need paralel timers with delays.
I would be very happy to hear your solution.
I described my problem here:
Best,
Bab
One option can be to have a single timer, that runs and checks if there are new images from each of the camera (or if it's the time set by user to take new image from any given camera). It can then execute the code on the new images it find. If no new images then it will just exit.
Another option can be, that you have while loop linked to the state of a toggle button. As long as the toggle button is pressed, it will continue to check all cameras one at a time in a for loop. Once the user uncheck the toggle button, it will stop running.
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.
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

Products

Release

R2019a

Asked:

on 18 Aug 2019

Commented:

on 8 Apr 2020

Community Treasure Hunt

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

Start Hunting!