Repeating timer doesn't work

Hello,
I'd like to ask you for help.
I'm currently preparing simple application for an instrument which I use in the lab.
Basicaly you input values in through some form and the instrument sets (changes) voltage to the value you input and keeps it for the time you input.
I tried to use for loop in this case, at first you create an matrix of inputs for example Ut = (3x2) matrix.
Now I created a for loop which I expected to execute operations in function zobraz after some time in this case three times. for cnt = 1:ValCnt t = timer; t.StartDelay = Ut(cnt,2); t.TimerFcn = {@zobraz, Ut}; start(t); end
Basicaly if Ut = (1 10; 5 18; 3 14), I'd like to run the application with value 1 for ten seconds then change the value from 1 to 5 and run the application for 18 seconds and then change the value from 5 to 3 and run the application for 14 seconds. But when I use the code above it executes first operation after some random time, second operation goes off immediately after the first one and the third one is executed after some time, but basicaly times are not similar to my input.
I hope its understandable and that you can help me with this problem, I'm getting pretty frustated over this thing and I would really appreciate your help.
Thank you very much.

2 Comments

Markup makes it easier to read the code. Use the {}Code button
for cnt = 1:ValCnt
t = timer;
t.StartDelay = Ut(cnt,2);
t.TimerFcn = {@zobraz, Ut};
start(t);
end
Jan
Jan on 5 Jul 2013
Edited: Jan on 5 Jul 2013
What does "after some random time" exactly mean? Minutes or milliseconds?

Sign in to comment.

 Accepted Answer

function TimerTest
Ut = [1 10; 5 18; 3 14];
for cnt = 1:3
t = timer;
t.StartDelay = Ut(cnt,2);
t.TimerFcn = {@show, cnt};
start(t);
end
fprintf('%g ', clock);
fprintf('\n');
function show(TimerH, EventData, cnt)
fprintf('%d: ', cnt);
fprintf('%g ', clock);
fprintf('\n');
Results:
1: 2013 7 7 22 31 12.419
3: 2013 7 7 22 31 16.405
2: 2013 7 7 22 31 20.416
So I do not see any unexpected behavior. Therefore I assume, that the problems you observe are found in the function zobraz.
Remark: There is a certain delay between the timers cause by starting them in a loop. So better start them together after the loop:
for cnt = 1:3
t(cnt) = timer;
t(cnt).StartDelay = Ut(cnt,2);
t(cnt).TimerFcn = {@show, cnt};
end
start(t)
Then you get:
1: 2013 7 7 22 33 41.463
3: 2013 7 7 22 33 45.45
2: 2013 7 7 22 33 49.449

3 Comments

Thanks a lot, I got some lag problem with my computer, therefore everything went in a weird way. Our lab computer was getting pretty busy lately and we found that we were having some hw problems, after we changed the component that was causing problems everything runs smoothly. Although, I finally decided to change the approach a little bit as you suggested starting timers at the same time. Where I'd like to continue my question if I dont bother you too much.
I was just thinking about how is matlab good with situation when more then one or a few timers are actually active? I didn't find details on this, but when you start let's say twenty timers, or even more like one thousand timers (through loop mentioned in your answer). Does matlab initiates one thousand timers at the same time and then goes with one thousand of separate times or does it use one time and just proceeds every TimerFcn at the designated time? I mean it must require a lot of memory and/or processing time to count one thousand times to some time value, using one timer and just processing functioncs every designated time seems to me more advantageous then going with one thousand of separate time objects, is the question clearer this way?
And I have another corresponding question to this. In case that matlab goes with one thousand separate timers wouldn't be better to go with something like this (below)?
t = timer; index = 1; time = 0; t.ExecutionMode = 'fixedRate'; t.Period = 1; t.StartDelay = 1; t.TimerFcn = {@timing}; start(t);
function timing time = time+1; if time = Ut(index, 2) voltage = fprintf(obj, 'VOLT', Ut(index,1)); % sets voltage to the value % from first column of Ut matrix index = index+1; elseif index > m where m is number of rows in Ut matrix stop(t);
I don't know if the code is correct, I was having some issues with variables but solved it in the lab but right now I'm not on the lab computer but home. Basicaly what's the most important is the idea not the code itself. Wouldn't it be better to make it this way to execute the command only with one timer (time is saved as time variable with period one second, therefore in time is saved how many seconds have the whole thing been working then using one thousand of timer objects?
Thank you again in advance.
Starting one timer instead of 1000 needs much less resources. So if it is possible, as in your case, prefer this.
You do not need to store time persistently in the timer's UserData, but can use the timer's property TasksExecuted for counting the number of calls.
Hi Jan
Came across this old post. The TasksExcuted property is exactly what I was looking for to solve my problem. Thanks so much!

Sign in to comment.

More Answers (0)

Asked:

on 4 Jul 2013

Commented:

Jon
on 10 Dec 2021

Community Treasure Hunt

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

Start Hunting!