MATLAB: Repeating timer doesn’t work

forinstrumentlooptimer

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 = (3×2) 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.

Best 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