Pause function in matlab for 1 millisecond

1.305 Ansichten (letzte 30 Tage)
Juan
Juan am 8 Mai 2012
Beantwortet: Walter Roberson am 24 Apr. 2023
Hi,
I need to pause my matlab program at GUI, because I'm using serial communication, but when I use pause command it only allows me to pause it for 0.01 seconds.
pause(0.01) %in seconds
The thing I need, for example:
pause(0.001) %in seconds, but actually doesn't works
Anyone knows how to pause matlab for 1 millisecond? Thanks for read and answer.
Greetings.
  2 Kommentare
Walter Roberson
Walter Roberson am 17 Jan. 2017
Pause is on by default.
On MS Windows, the resolution is only 0.01 seconds and using "pause on" does not change that.

Melden Sie sich an, um zu kommentieren.

Antworten (9)

Jan
Jan am 24 Okt. 2015
Bearbeitet: Jan am 24 Okt. 2015
There is an accurate timer in Java, which can be called directly in Matlab:
java.lang.Thread.sleep(duration*1000) % in mysec!

AndreasDerFuchs
AndreasDerFuchs am 4 Mär. 2016
The attached pauses() matlab function combines the above ideas.
It can pause with an accuracy of 0.03 ms on my PC, without using too much CPU-bandwidth, as opposed to an accuracy of 0.8 ms with java.lang.Thread.sleep(ms), or the even worse accuracy of 15 ms with pause().
I've tested the accuracy with:
pauses(0); ii=1:300; d=ii/12345;
for i=ii;
t0=tic; pauses(d(i),t0); t(i)=toc(t0);
end;
fprintf('3 sigma accuracy = %.6f ms\n', std(t-d)*3000);
  2 Kommentare
Toby Dewhurst
Toby Dewhurst am 10 Jun. 2016
Works perfectly. Thanks for sharing!

Melden Sie sich an, um zu kommentieren.


Daniel Shub
Daniel Shub am 8 Mai 2012
Getting millisecond timing accuracy is extremely difficult in most programming languages since the underlying clocks are just not that accurate. It really is a question of how much jitter you can tolerate.
That said, the "serial port" tag makes me think you should look at callback functions.

Andreas Goser
Andreas Goser am 8 Mai 2012
Bearbeitet: John Kelly am 27 Mai 2014
While Jakob's answer is a correct answer to your specific question, you might want to do something different, as the PAUSE command really stops MATLAB from doing stuff.

Malcolm Lidierth
Malcolm Lidierth am 19 Mai 2016
Bearbeitet: Malcolm Lidierth am 19 Mai 2016
Pause does more than cause a sleep (see the docs) e.g. it flushes the AWT/Swing EDT. The minimum delay will therefore reflect what needs to be done in the background. undocumentedmatlab.com has several relevant posts (see drawnow/pause)

Jakob Sørensen
Jakob Sørensen am 8 Mai 2012
Doesn't work how? And what Matlab version are you using? In R2011b, running Windows 7, I get the following results
>> tic;pause(0.001);toc;
Elapsed time is 0.009849 seconds.
Which is reasonably close to 1 ms.
  7 Kommentare
Walter Roberson
Walter Roberson am 2 Nov. 2015
R2015a on OS-X:
g = @() pause(0.001);
timeit(g, 0)
shows outputs between 0.001255 and 0.001404. (Be sure to use 0 as the second argument or else you end up measuring the time to return the "state" of the timer.)
The result is consistently higher than 1 1/4 millisecond and less than 1 1/2 millisecond in the tests I have done this way. Note that
tic(); pause(0.001); toc()
at the command line is not completely representative due to differences in what is JIT (Just In Time compiled)

Melden Sie sich an, um zu kommentieren.


Marcel Kraemer
Marcel Kraemer am 5 Feb. 2013
Bearbeitet: Marcel Kraemer am 5 Feb. 2013
Hi Guys,
I had the same problem as you and I couldn't find an answer on the internet. So I tried following solution which works percetly well for me.
function delay(seconds)
% function pause the program
% seconds = delay time in seconds
tic;
while toc < seconds
end
end
Cheers, Marcel
  1 Kommentar
KnowledgeSeeker
KnowledgeSeeker am 20 Jun. 2014
Thank you Marcel. Works for me too...

Melden Sie sich an, um zu kommentieren.


Andreas Sprenger
Andreas Sprenger am 27 Sep. 2013
Hi,
on a 32bit Windows System you may use a kernel function. It works quite accurate, I've tested it against hardware timer. Unfortunately I haven't found a solution on 64bit Win7. Any hints are welcome.
Cheers
Andreas
Here an example for 32bit Windows:
if ~libisloaded('QPerf')
% loads kernel functions for time measurement
loadlibrary ('kernel32.dll', @QueryPerformance, 'alias', 'QPerf')
end
% data structure for QueryPerformance...
Value.LowPart = uint32(0);
Value.HighPart = uint32(0);
QStruct = libstruct('s_ULARGE_INTEGER', Value);
% Get performance of the computer
[xval, QFreq] = calllib('QPerf', 'QueryPerformanceFrequency', QStruct);
Frequency = QFreq.HighPart*2^32 + QFreq.LowPart;
% Get t0
[~, QCounter] = calllib('QPerf', 'QueryPerformanceCounter', QStruct);
t0 = (QCounter.HighPart*2^32 + QCounter.LowPart) / Frequency * 1000;
t1 = t0;
% example: wait a second
while t1 - t0 < 1000
[~, QCounter] = calllib('QPerf', 'QueryPerformanceCounter', QStruct);
t1 = (QCounter.HighPart*2^32 + QCounter.LowPart) / Frequency * 1000;
end
% -----
function [methodinfo,structs,enuminfo] = QueryPerformance
%This function was generated by the perl file prototypes.pl called from loadlibary.m on Wed Apr 5 19:40:14 2006
%perl options:'win.i -outfile=QueryPerformance
ival={cell(1,0)}; % change 0 to the actual number of functions to preallocate the data.
fcns=struct('name',ival,'calltype',ival,'LHS',ival,'RHS',ival,'alias',ival);
structs=[];enuminfo=[];fcnNum=1;
% BOOL _stdcall QueryPerformanceCounter(LARGE_INTEGER *);
fcns.name{fcnNum}='QueryPerformanceCounter';
fcns.calltype{fcnNum}='stdcall';
fcns.LHS{fcnNum}='int32';
fcns.RHS{fcnNum}={'s_ULARGE_INTEGERPtr'};
fcnNum = fcnNum+1;
% BOOL _stdcall QueryPerformanceFrequency(LARGE_INTEGER *);
fcns.name{fcnNum}='QueryPerformanceFrequency';
fcns.calltype{fcnNum}='stdcall';
fcns.LHS{fcnNum}='int32';
fcns.RHS{fcnNum}={'s_ULARGE_INTEGERPtr'};
fcnNum = fcnNum+1;
structs.s_ULARGE_INTEGER.packing=8;
structs.s_ULARGE_INTEGER.members=struct('LowPart', 'uint32', 'HighPart', 'uint32');
methodinfo = fcns;
  2 Kommentare
Jan
Jan am 24 Okt. 2015
Verschoben: DGM am 24 Apr. 2023
Please do not post a question in the section for answers. See my new answer...

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 24 Apr. 2023
The Windows task scheduler runs every 15 ms, so you cannot get 0.001 resolution without resorting to something like busy-wait.
See the extended discussion, with specific tests, at

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by