Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Pause function in matlab for 1 millisecond

Asked by Juan on 8 May 2012
Latest activity Commented on by KnowledgeSeeker on 20 Jun 2014


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.





7 Answers

Answer by Daniel on 8 May 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.


Answer by Andreas Goser on 8 May 2012
Edited by John Kelly on 27 May 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.


Andreas Goser
Answer by Marcel Kraemer on 5 Feb 2013
Edited by Marcel Kraemer on 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
while toc < seconds

Cheers, Marcel

1 Comment

KnowledgeSeeker on 20 Jun 2014

Thank you Marcel. Works for me too...

Marcel Kraemer
Answer by Jakob Sørensen on 8 May 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.


Jakob Sørensen on 9 May 2012

Jan -> wops, you're right, saw it as three zeros after the dot. Then just ignore my answer :p

Daniel on 9 May 2012

@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 on 4 Jun 2012

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!

Jakob Sørensen
Answer by Richard Plant on 13 May 2012

I guess it depends on what you want to do but there's a subtle difference between accuracy and precision. Even using APIs you'll be millisecond precise but not accurate. That is, units of milliseconds but not that you're reading the timer at the right time.

Plus if you're trying to time say a visual display to the millisecond then you're likely to be out of luck. PC's, Macs and Linux will all have similar problems as they use the same hardware. Even RTOS systems won't help you all that much as soon as you interact with a standard TFT or keyboard.

For some background on the issues take a look at: and read about our Black Box ToolKit which helps users achieve millisecond timing accuracy in experimental work.

1 Comment

Daniel on 14 May 2012

As is, I would say that this answer seems a lot like spam and falls into the editor guidelines ( for deletion because of "Off-topic content not related to MATLAB or any MathWorks products". This answer would be a lot better if you provided some MATLAB code that "pauses for 1 ms" and then then showed measurements from you blackbox that the pause was not 1 ms. Ideally, give the tags chosen by the OP, the MATLAB code should do something with the serial port (from my quick glance at the documentation of blackbox it is not clear that blackbox can measure and create serial port "events").

Richard Plant
Answer by Andreas Sprenger on 27 Sep 2013


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.



Here an example for 32bit Windows:

if ~libisloaded('QPerf')
    % loads kernel functions for time measurement
    loadlibrary ('kernel32.dll', @QueryPerformance, 'alias', 'QPerf')
% 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;

% -----

function [methodinfo,structs,enuminfo] = QueryPerformance
%This function was generated by the perl file 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.
%  BOOL _stdcall QueryPerformanceCounter(LARGE_INTEGER *);{fcnNum}='QueryPerformanceCounter'; 
fcnNum = fcnNum+1;
%  BOOL _stdcall QueryPerformanceFrequency(LARGE_INTEGER *);{fcnNum}='QueryPerformanceFrequency'; 
fcnNum = fcnNum+1;
structs.s_ULARGE_INTEGER.members=struct('LowPart', 'uint32', 'HighPart', 'uint32');
methodinfo = fcns;


Andreas Sprenger
Answer by Andreas Sprenger on 27 Sep 2013


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 Sprenger

Contact us