MATLAB Answers

0

timer function is not executing the function i am providing ,please help me with this?

Asked by Shivank Anchal on 10 Jun 2019
Latest activity Answered by Steven Lord
on 10 Jun 2019
this is in script window:
function ritik
for x=1:10
start(t)
stop(t)
disp(x)
end
this is in command window:
>> global t
>> t=timer('timerfcn',ritik,'startdelay',2)
Error using ritik
Too many output arguments.
>>

  0 Comments

Sign in to comment.

3 Answers

Answer by LO
on 10 Jun 2019
Edited by LO
on 10 Jun 2019

ritik does not seem to be a matlab function, could you post your code ?

  0 Comments

Sign in to comment.


Answer by Shivank Anchal on 10 Jun 2019

function ritik
for x=1:10
start(t)
stop(t)
disp(x)
end
>> global t
>> t=timer('timerfcn',ritik,'startdelay',2)

  3 Comments

you might need to specify arguments for the function you are creating or at least avoid putting delay after the ritik, as it is probably read as a callback (which does not exist in your function code).
also I think you might add the @ before your callback function.
try this
global t
%t=timer('Timerfcn',@ritik,'startdelay',2)
ritik(t)
function ritik(t)
for x=1:10
start(t)
stop(t)
disp(x)
end
end
I actually am not sure what you want to achieve.
maybe you want the stop time to be displayed ? It is not clear what start(t) and stop(t) will achieve with this code. if you want to count the time spent in between each start of the for cycle you could also use "tic" and "toc" .. or use the functions clock or datetime (datetime now gives you the instant time)
>> t=timer('timerfcn',@(x,y)disp(rand),'executionmode','fixedrate','period',1)
t =
Timer Object: timer-39
Timer Settings
ExecutionMode: fixedRate
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(x,y)disp(rand)
ErrorFcn: ''
StartFcn: ''
StopFcn: ''
>> start(t)
0.8147
0.9058
0.1270
0.9134
0.6324
0.0975
0.2785
>> stop(t)
This code print random numbers,after every one 1 second....NOW i want to print the whole numbers ,each printing one second after printing a number.you can run the above code to understand me.

Sign in to comment.


Answer by Steven Lord
on 10 Jun 2019

t=timer('timerfcn',ritik,'startdelay',2)
This attempts to call the ritik function with 0 input arguments and 1 output argument and use the output argument from that call as the TimerFcn for the timer being created on this line. But your ritik function cannot return any output arguments, so MATLAB throws an error.
If you want MATLAB to call ritik as the TimerFcn for the timer specify it as a function handle.
t=timer('timerfcn',@ritik,'startdelay',2)
However, as stated in the "Creating Callback Functions" section on this documentation page, "When you create a callback function, the first two arguments must be a handle to the timer object and an event structure." Your ritik function does not accept any input arguments, and so does not satisfy the requirements imposed upon a TimerFcn by the timer object. You could use an anonymous function as an "adapter" (see the table in the "Specifying the Value of Callback Function Properties" on the documentation page I linked above) to bridge the gap between the signature the timer object expects its TimerFcn to have and the signature your ritik function actually has.
But looking more closely at your ritik function, I'm not sure what exactly you're trying to do with your timer. It won't have access to the global variable t (and I strongly recommend avoiding global variables!) and all it does is try to start and stop the timer repeatedly.
If you explain why you're trying to use a timer in this situation we may be able to help you achieve that goal.

  0 Comments

Sign in to comment.