Pause function in matlab for 1 millisecond

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

chandan kumar
chandan kumar 2017년 1월 17일
편집: Walter Roberson 2023년 4월 24일
Hello there,
First use:
pause on % to enable pause function
Now you can use pause function:
pause(x); // delay in seconds (x is non-negative real number)
Pause is on by default.
On MS Windows, the resolution is only 0.01 seconds and using "pause on" does not change that.

댓글을 달려면 로그인하십시오.

답변 (9개)

Jan
Jan 2015년 10월 24일
편집: Jan 2015년 10월 24일

11 개 추천

There is an accurate timer in Java, which can be called directly in Matlab:
java.lang.Thread.sleep(duration*1000) % in mysec!
AndreasDerFuchs
AndreasDerFuchs 2016년 3월 4일

9 개 추천

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

akin hunerli
akin hunerli 2016년 5월 14일
hi Hamid Reza saadati, i have the problem with the pause command working on a guide interface then i realised that we have to add a pause command end of the code.
Toby Dewhurst
Toby Dewhurst 2016년 6월 10일
Works perfectly. Thanks for sharing!

댓글을 달려면 로그인하십시오.

Daniel Shub
Daniel Shub 2012년 5월 8일

4 개 추천

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 2012년 5월 8일
편집: John Kelly 2014년 5월 27일

3 개 추천

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 2016년 5월 19일
편집: Malcolm Lidierth 2016년 5월 19일

2 개 추천

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 2012년 5월 8일

1 개 추천

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

Jan
Jan 2012년 5월 8일
Reasonably close to 1ms?! It is almost 10 times longer.
Juan
Juan 2012년 5월 8일
Hi,
I need 1 millisecond or less... I'm using R2009a and I got this with tic and toc:
>> tic;pause(0.001);toc;
Elapsed time is 0.018589 seconds.
>> tic;pause(0.001);toc;
Elapsed time is 0.020876 seconds.
>> tic;pause(0.001);toc;
Elapsed time is 0.008370 seconds.
>> tic;pause(0.001);toc;
Elapsed time is 0.016282 seconds.
>> tic;pause(0.001);toc;
Elapsed time is 0.005483 seconds.
>> tic;pause(0.001);toc;
Elapsed time is 0.003235 seconds.
I'm not sure how to use timer objects or serial callback functions to do this delay... Any examples? Thanks for your answers
Jan -> wops, you're right, saw it as three zeros after the dot. Then just ignore my answer :p
Daniel Shub
Daniel Shub 2012년 5월 9일
@Juan, can you include some more detail about what you are doing and what you are trying to accomplish. Please don't respond in the comments, but rather edit your question using markup. From your tags I am guessing you have a loop in which you read from the serial port (possibly do something with the data) and then pause, but this is more or less a wild guess.
Christin
Christin 2012년 6월 4일
Juan I am experiencing the exact same problem! I've found that ~0.0156 sec is the fastest "pause" can go. I'd love to do a for-loop at 100Hz but can't another way to do it. If you ever find a solution to this problem please share!
timo
timo 2015년 11월 2일
Elapsed time is 0.015487 seconds. Wtf
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)

댓글을 달려면 로그인하십시오.

Marcel Kraemer
Marcel Kraemer 2013년 2월 5일
편집: Marcel Kraemer 2013년 2월 5일

1 개 추천

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

댓글 수: 2

Thank you Marcel. Works for me too...
Oren
Oren 2025년 2월 27일
편집: Oren 2025년 2월 27일
I generated a repetative analog boxcar/square signal using a simple DAQ (USB6008) with the following scheduled commands from MATLAB: 1 ms 'on' time and 10ms 'off' time. I fed the analog signal to a scope and compared the three methods: your delay(), built-in pause() and java.lang.Thread.sleep(). your method is significantly more stable than the other two.
Using MATLAB 2019a and NI-USB6008. Here is the code I used:
s = daq.createSession('ni');
s.Rate = 10000;
analog_output_1 = addAnalogOutputChannel(s,'Dev3','ao0','Voltage');
digital_output_1.Name = 'trigger_out';
for ind = 1:1e4
trigger(s);
delay(0.01);
% pause(0.01);
% java.lang.Thread.sleep(10);
end
s.release;
function trigger(s)
s.outputSingleScan(1)
delay(0.001);
% pause(0.001);
% java.lang.Thread.sleep(1);
s.outputSingleScan(0)
end
function delay(seconds)
% function pause the program
% seconds = delay time in seconds
tic;
while toc < seconds
end
end

댓글을 달려면 로그인하십시오.

Andreas Sprenger
Andreas Sprenger 2013년 9월 27일

1 개 추천

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

Andreas Sprenger
Andreas Sprenger 2013년 9월 27일
이동: DGM 2023년 4월 24일
Hi,
searching for a solution on 64bit Matlab I came across a workaround. Edit the prototype and change fcns.calltype from 'stdcall' (most common on 32bit Windows systems) to 'cdecl' (C type declaration). It works both on 32bit and 64bit Matlab systems. On 2013a there is a warning that the loader file will not be supported in future.
Andreas
Jan
Jan 2015년 10월 24일
이동: DGM 2023년 4월 24일
Please do not post a question in the section for answers. See my new answer...

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2023년 4월 24일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

질문:

2012년 5월 8일

편집:

2025년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by