I am an amateur Matlab User who is attempting to write a code to run a specific function call every 10ms for a time span of 1 second. I am having trouble trying to get something to run for the exact amount of time; I have tried to use Tic and Toc but that's in seconds (I need millisecond precision). Here is some very basic code I have been playing with to try and get readings using a function called getvelocity. Any help would be appreciated, thanks!
function [ velocity ] = Vel()
i=1;
timerID=tic;
while (toc(timerID) <=2);
[v(i)]=vpx_GetTotalVelocity;
disp (v(i));
i=i+1;
end
velocity=mean(v);
end
The code above runs for two seconds; however, I want to run in ms precision.
Assuming the function you have is fast enough (not a trivial assumption) you can achieve it like so:
tic
for t = 0.01:0.01:1 %If you want the first function call to start right away you can change this to 0:0.01:0.99
while toc < t
end
t %Put your function call here.
end
Note that 0.01 sec is 10 ms
Related
Is it possible to put the calling of function into another thread? I tried spmd in the function but i get error saying that
MATLAB cannot determine whether "System" refers to a function or
variable.
tic
Speak('Testing');
toc
...next line of code...
function Speak(text)
spmd
NET.addAssembly('System.Speech');
speak = System.Speech.Synthesis.SpeechSynthesizer;
speak.Volume = 100;
speak.Speak(text);
end
end
I get elapsed time of approximately 2 seconds, which is pretty long for this purpose. I would prefer the speech continuing in the background and proceed to the next line of code in the meantime.
For example if there is a MATLAB program as follows:
duration = 1 % minute
i=1
while i<1000
[X,Y] = ginput(1)
i = i+1;
end
Is there any way to terminate the execution of this program or getting out of the loop when it reaches to the assigned amount of time (1 minute in this case) in such a situation in which continuation of the loop needs the user intervention (in this case clicking on any point on the plotted figure)?
If you want to do a process within your loop for a minimum of a minute, you can do it like this:
start = now
while now - start < 60/60/24
%do something for a minute
end
As a commenter above said, ginput will wait indefinitely so don't plan on that working.
Is there a way to measure the time at which an event occurs (without using sample or anything similar) in Modelica? something like the tic toc command in MATLAB? what I would like to see is the difference in time when different events happen. For example, in the following sample code, is there a way to see the elapsed time using test_time1 and test_time2?
when event1 then
a:=2;
event2:= true;
test_time1 := time;
end when;
when event2 then
a:= 5;
test_time2 := time;
end when;
abs(test_time2-test_time1) should do it if you do not know which occurs first. Note that this would be the simulation time, not real (wall) time. If you want to measure the real time it takes for a simulation to trigger the two events, you need to use external C functions calling your own tic and toc.
I want to call function A in a program. This function has a for loop inside and I'd like to shorten amount of iterations but not just changing the upper limit. How could I do it in this case? I was thinking if Matlab is capable of doing something like: one timer inside a function (or maybe inside a loop) and second in the main program that calls this function? But only rough idea, I'm a beginner. Please feed back if this is good idea and how could it be implemented?
thank you!
It sounds like you're talking about having a maximum elapsed time condition in your loop, something along the lines of,
MAX_T = 10;
tic;
for n=1:NMAX
% Call your loop functions
.
.
% Break if youve spent too much time in the loop
if toc > MAX_T; break; end;
end
There are also ways of optimizing this, such as only checking the value of toc every N iterations.
I am using tic-toc function in my Matlab project as many places. The output time can be 331.5264 or 1234.754 seconds, etc. Can I output this is minutes format? For eg. 5 minutes and 30.6 seconds? Thanks!
All you have to do is capture the output from toc (instead of letting it display its default output), then create an output yourself using the functions fprintf, floor, and rem:
tStart = tic;
% Do some things...
tEnd = toc(tStart);
fprintf('%d minutes and %f seconds\n', floor(tEnd/60), rem(tEnd,60));
While tic and toc do not have any way to display values in minutes, you can process the data slightly before displaying it. Check out the following link to a seconds to hour/minute converter.
Usage would be as follows:
tic
% Do something
time_str = SECS2HMS(toc)
disp(time_str)
I will try this out when I get back on my Windows VM. Hope this helps.
EDIT
You could use the datestr and datenum function built into Matlab as well in the following way. Note that I have not tried this code either, but the link reminded me of the syntax on how to do it (without bringing up Matlab)
tic
%Do something
t=toc;
disp(datestr(datenum(0,0,0,0,0,t),'HH:MM:SS'))
The easiest way I've found is to use:
TIME = tic;
% do calculations which take TIME to complete
fprintf('This took %s', duration([0, 0, toc(TIME)]));
(toc() returns the time from the stopwatch in seconds, and duration() expects a three-column matrix expressing a time duration in [hours, minutes, seconds] format).
This has the nice property of keeping most of the timing calculations out of sight.
Hope this helps.
This one suited me:
disp(['Elapsed time is ' num2str(round(toc/60,1)) ' minutes.'])