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

Thread Subject:
how to make a delay in a "for" cycle?

Subject: how to make a delay in a "for" cycle?

From: Gabriel Ortega Rosero

Date: 7 Apr, 2013 16:10:08

Message: 1 of 11

Hi, I need to do matlab data acquisition, but I need control the rate and amount of data stored in a vector. I made a "for cycle", but I need that the cycle takes place each unit time defined, up to the total acquisition time. I mean, if t=1, I need 500 samples stored in a second, one each 0.002ms. How I can do that?. Thanks!

clc
t=1;
v1=zeros(1,500*t);
delete(instrfind({'Port'},{'COM3'}))
s = serial('COM3');
set(s,'BaudRate',9600);
s.inputbufferSize = 10000;

delay= wait time = frecuency of data
fopen(s);

for i=1:t*500;

a=fscanf(s,'%d');
v1(i)=a
i=i+1;
wait time
end
fclose(s);

Subject: how to make a delay in a "for" cycle?

From: dpb

Date: 7 Apr, 2013 16:52:03

Message: 2 of 11

On 4/7/2013 11:10 AM, Gabriel Ortega Rosero wrote:
> Hi, I need to do matlab data acquisition, but I need control the rate
> and amount of data stored in a vector. I made a "for cycle", but I need
> that the cycle takes place each unit time defined, up to the total
> acquisition time. I mean, if t=1, I need 500 samples stored in a second,
> one each 0.002ms. How I can do that?. Thanks!
>
...

> s = serial('COM3');
> set(s,'BaudRate',9600);
...

Well, at 9600 baud if the data stream is 10 characters/value it will be
about 100 values/sec maximum thruput w/ no overhead so you don't need to
wait but to hurry up.

Only if you can control the data acq unit to buffer on board and control
it's sampling rate can you do 500 Hz this way.

--

Subject: how to make a delay in a "for" cycle?

From: anyone

Date: 7 Apr, 2013 17:07:02

Message: 3 of 11

On Sun, 07 Apr 2013 11:52:03 -0500, dpb wrote:

> On 4/7/2013 11:10 AM, Gabriel Ortega Rosero wrote:
>> Hi, I need to do matlab data acquisition, but I need control the rate
>> and amount of data stored in a vector. I made a "for cycle", but I need
>> that the cycle takes place each unit time defined, up to the total
>> acquisition time. I mean, if t=1, I need 500 samples stored in a
>> second,
>> one each 0.002ms. How I can do that?. Thanks!
>>
> ...
>
>> s = serial('COM3');
>> set(s,'BaudRate',9600);
> ...
>
> Well, at 9600 baud if the data stream is 10 characters/value it will be
> about 100 values/sec maximum thruput w/ no overhead so you don't need to
> wait but to hurry up.
>
> Only if you can control the data acq unit to buffer on board and control
> it's sampling rate can you do 500 Hz this way.

...or, develop a 'request - send - ack' sequence. don't know whether that is possible with DAQ, but it'd likely be more
reliable.

Subject: how to make a delay in a "for" cycle?

From: Gabriel Ortega Rosero

Date: 7 Apr, 2013 19:04:11

Message: 4 of 11

dpb <none@non.net> wrote in message <kjs87h$v7i$1@speranza.aioe.org>...
> On 4/7/2013 11:10 AM, Gabriel Ortega Rosero wrote:
> > Hi, I need to do matlab data acquisition, but I need control the rate
> > and amount of data stored in a vector. I made a "for cycle", but I need
> > that the cycle takes place each unit time defined, up to the total
> > acquisition time. I mean, if t=1, I need 500 samples stored in a second,
> > one each 0.002ms. How I can do that?. Thanks!
> >
> ...
>
> > s = serial('COM3');
> > set(s,'BaudRate',9600);
> ...
>
> Well, at 9600 baud if the data stream is 10 characters/value it will be
> about 100 values/sec maximum thruput w/ no overhead so you don't need to
> wait but to hurry up.
>
> Only if you can control the data acq unit to buffer on board and control
> it's sampling rate can you do 500 Hz this way.
>
> --Do you say that I have to change the Bundrate value? I did't understand

Subject: how to make a delay in a "for" cycle?

From: dpb

Date: 7 Apr, 2013 19:21:27

Message: 5 of 11

On 4/7/2013 2:04 PM, Gabriel Ortega Rosero wrote:
> dpb <none@non.net> wrote in message <kjs87h$v7i$1@speranza.aioe.org>...
>> On 4/7/2013 11:10 AM, Gabriel Ortega Rosero wrote:
>> > Hi, I need to do matlab data acquisition, but I need control the rate
>> > and amount of data stored in a vector. I made a "for cycle", but I need
>> > that the cycle takes place each unit time defined, up to the total
>> > acquisition time. I mean, if t=1, I need 500 samples stored in a
>> second,
>> > one each 0.002ms. How I can do that?. Thanks!
>> >
>> ...
>>
>> > s = serial('COM3');
>> > set(s,'BaudRate',9600);
>> ...
>>
>> Well, at 9600 baud if the data stream is 10 characters/value it will
>> be about 100 values/sec maximum thruput w/ no overhead so you don't
>> need to wait but to hurry up.
>>
>> Only if you can control the data acq unit to buffer on board and
>> control it's sampling rate can you do 500 Hz this way.
>>
>> --Do you say that I have to change the Bundrate value? I did't understand

I'm telling you that if your data are formatted like SX.XXXES00 or
somesuch, that's 10 characters/value and the rate at which those
characters can be transmitted at 9600 baud means you'll only get roughly
100 of them/sec max.

If your device runs at higher baudrates then you can expect thruput
roughly proportional to the ratio but that if you really want 500 hz
sample rate and it needs to be accurately sampled then you're not going
to accomplish it by such a means.

What is the actual hardware device?

--

Subject: how to make a delay in a "for" cycle?

From: Gabriel Ortega Rosero

Date: 7 Apr, 2013 20:52:09

Message: 6 of 11

dpb <none@non.net> wrote in message <kjsgvl$4r1$1@speranza.aioe.org>...
> On 4/7/2013 2:04 PM, Gabriel Ortega Rosero wrote:
> > dpb <none@non.net> wrote in message <kjs87h$v7i$1@speranza.aioe.org>...
> >> On 4/7/2013 11:10 AM, Gabriel Ortega Rosero wrote:
> >> > Hi, I need to do matlab data acquisition, but I need control the rate
> >> > and amount of data stored in a vector. I made a "for cycle", but I need
> >> > that the cycle takes place each unit time defined, up to the total
> >> > acquisition time. I mean, if t=1, I need 500 samples stored in a
> >> second,
> >> > one each 0.002ms. How I can do that?. Thanks!
> >> >
> >> ...
> >>
> >> > s = serial('COM3');
> >> > set(s,'BaudRate',9600);
> >> ...
> >>
> >> Well, at 9600 baud if the data stream is 10 characters/value it will
> >> be about 100 values/sec maximum thruput w/ no overhead so you don't
> >> need to wait but to hurry up.

The device is a arduino one, do you know it? and the code inside it is this:

int out1 = 0;
void setup() {
   // begin serial port
   Serial.begin(9600);}
 
void loop() {
   // read pin
   out1 = analogRead(A0);
    // send
   Serial.println(out1);
     // wait
   delay(1);}
The time delay is in ms, then de devie is reading and sending data at 1Khz, is that correct?

Subject: how to make a delay in a "for" cycle?

From: dpb

Date: 7 Apr, 2013 21:23:17

Message: 7 of 11

On 4/7/2013 3:52 PM, Gabriel Ortega Rosero wrote:
> dpb <none@non.net> wrote in message <kjsgvl$4r1$1@speranza.aioe.org>...
>> On 4/7/2013 2:04 PM, Gabriel Ortega Rosero wrote:
>> > dpb <none@non.net> wrote in message <kjs87h$v7i$1@speranza.aioe.org>...
>> >> On 4/7/2013 11:10 AM, Gabriel Ortega Rosero wrote:
>> >> > Hi, I need to do matlab data acquisition, but I need control the
>> rate
>> >> > and amount of data stored in a vector. I made a "for cycle", but
>> I need
>> >> > that the cycle takes place each unit time defined, up to the total
>> >> > acquisition time. I mean, if t=1, I need 500 samples stored in a
>> >> second,
>> >> > one each 0.002ms. How I can do that?. Thanks!
>> >> >
>> >> ...
>> >>
>> >> > s = serial('COM3');
>> >> > set(s,'BaudRate',9600);
>> >> ...
>> >>
>> >> Well, at 9600 baud if the data stream is 10 characters/value it will
>> >> be about 100 values/sec maximum thruput w/ no overhead so you don't
>> >> need to wait but to hurry up.
>
> The device is a arduino one, do you know it? and the code inside it is
> this:
>
> int out1 = 0;
> void setup() {
> // begin serial port
> Serial.begin(9600);}
>
> void loop() {
> // read pin
> out1 = analogRead(A0);
> // send
> Serial.println(out1);
> // wait
> delay(1);}
> The time delay is in ms, then de devie is reading and sending data at
> 1Khz, is that correct?

No, heard of it but I don't know it otomh; you got a web url?

And, yes, while the resolution of the argument in C library delay
function is in msec, that doesn't mean the PC RTC resolution will be
anything close to that; only that the delay will be at least 1 msec.
More likely it will be about 10.

Plus, it also only waits from the time the time the code in

out1=analogRead(A0);
Serial.println(out1);

has executed until the next beginning of the call to the next
analogRead() call; how long it takes for that to happen is dependent on
the amount of data being transmitted in the message and the overhead in
the rest of the handshaking w/ the device and so on and so forth...

The above is purely a round-robin loop that essentially runs at whatever
speed it can but is highly unlikely imo to be close to 500 Hz if the
data message is of any length at all and the timer is implemented in the
C library w/ the HS timer and much other luck going on...

What is the format of the data that you expect?

--

Subject: how to make a delay in a "for" cycle?

From: dpb

Date: 8 Apr, 2013 01:07:32

Message: 8 of 11

On 4/7/2013 4:23 PM, dpb wrote:
> On 4/7/2013 3:52 PM, Gabriel Ortega Rosero wrote:
>> dpb <none@non.net> wrote in message <kjsgvl$4r1$1@speranza.aioe.org>...
>>> On 4/7/2013 2:04 PM, Gabriel Ortega Rosero wrote:
>>> > dpb <none@non.net> wrote in message
>>> <kjs87h$v7i$1@speranza.aioe.org>...
>>> >> On 4/7/2013 11:10 AM, Gabriel Ortega Rosero wrote:
>>> >> > Hi, I need to do matlab data acquisition, but I need control the
>>> rate
>>> >> > and amount of data stored in a vector. I made a "for cycle", but
>>> I need
>>> >> > that the cycle takes place each unit time defined, up to the total
>>> >> > acquisition time. I mean, if t=1, I need 500 samples stored in a
>>> >> second,
>>> >> > one each 0.002ms. How I can do that?. Thanks!
>>> >> >
>>> >> ...
>>> >>
>>> >> > s = serial('COM3');
>>> >> > set(s,'BaudRate',9600);
>>> >> ...
>>> >>
>>> >> Well, at 9600 baud if the data stream is 10 characters/value it will
>>> >> be about 100 values/sec maximum thruput w/ no overhead so you don't
>>> >> need to wait but to hurry up.
>>
>> The device is a arduino one, do you know it? and the code inside it is
>> this:
>>
>> int out1 = 0;
>> void setup() {
>> // begin serial port
>> Serial.begin(9600);}
>>
>> void loop() {
>> // read pin
>> out1 = analogRead(A0);
>> // send
>> Serial.println(out1);
>> // wait
>> delay(1);}
>> The time delay is in ms, then de devie is reading and sending data at
>> 1Khz, is that correct?
>
> No, heard of it but I don't know it otomh; you got a web url?
>
> And, yes, while the resolution of the argument in C library delay
> function is in msec, that doesn't mean the PC RTC resolution will be
> anything close to that; only that the delay will be at least 1 msec.
> More likely it will be about 10.
>
> Plus, it also only waits from the time the time the code in
>
> out1=analogRead(A0);
> Serial.println(out1);
>
> has executed until the next beginning of the call to the next
> analogRead() call; how long it takes for that to happen is dependent on
> the amount of data being transmitted in the message and the overhead in
> the rest of the handshaking w/ the device and so on and so forth...
>
> The above is purely a round-robin loop that essentially runs at whatever
> speed it can but is highly unlikely imo to be close to 500 Hz if the
> data message is of any length at all and the timer is implemented in the
> C library w/ the HS timer and much other luck going on...
>
> What is the format of the data that you expect?

OK, I looked up the Arduino analogRead() docs...it returns only a single
word holding the 10-bit A/D 0-1023 so there isn't a conversion to scaled
values going on that I presumed earlier. That speeds things up quite a
bit...they claim ~10 kHz but that is essentially 1/9600 so there's no
margin there at all.

What you can get in ML w/ the tight loop you'll just have to find out
experimentally.

How precise timing do you need? What are you going to be doing w/ the
data? If it's for something like spectral analysis or such where
precise sampling frequencies are critical it's not going to be
consistent enough between samples I'd expect even if you get
approximately the desired average sampling rate. If it's simply to
observe a phenomenon at roughly 500 Hz, then mayhaps you'll be
ok--assuming nothing is done to cause the PC to task context shift,
etc., etc, etc., while sampling.

I think the only way you'll be able to do what your original question
asked will be by trial timings to see how fast it runs and then figure
out what you need as a delay, if any.

Here is an example here of what happens w/ the "ordinary" RTC in a PC
and the PAUSE function in ML that relies on it...

 >> % try 2 msec pause...
 >> while 1,tic;pause(0.002),toc,end
Elapsed time is 0.024567 seconds.
Elapsed time is 0.005751 seconds.
Elapsed time is 0.015492 seconds.
Elapsed time is 0.015530 seconds.
Elapsed time is 0.015288 seconds.
...
Elapsed time is 0.015439 seconds.
Elapsed time is 0.015503 seconds.
Elapsed time is 0.015832 seconds.

NB: the average result is that of the 60 Hz RTC...

% A 10 msec gives same result....
 >> while 1,tic;pause(0.01),toc,end
Elapsed time is 0.004638 seconds.
Elapsed time is 0.014507 seconds.
Elapsed time is 0.015291 seconds.
Elapsed time is 0.015596 seconds.
...
Elapsed time is 0.015743 seconds.
Elapsed time is 0.015338 seconds.

Only see a change when > that resolution...
Try 20 mse>16.66...

 >> while 1,tic;pause(0.02),toc,end
Elapsed time is 0.021175 seconds.
Elapsed time is 0.030344 seconds.
Elapsed time is 0.031068 seconds.
...
Elapsed time is 0.030784 seconds.
Elapsed time is 0.031173 seconds.
Elapsed time is 0.031260 seconds.

And, to see the granularity, we'll
try 30 < 2*16.666....

 >> while 1,tic;pause(0.03),toc,end
Elapsed time is 0.020534 seconds.
Elapsed time is 0.030406 seconds.
Elapsed time is 0.031087 seconds.
Elapsed time is 0.030940 seconds.
Elapsed time is 0.031060 seconds.
Elapsed time is 0.031249 seconds.
Elapsed time is 0.031412 seconds.
...
Elapsed time is 0.030828 seconds.
Elapsed time is 0.030954 seconds.
Elapsed time is 0.031778 seconds.

So we get roughly 30 msec but it's the same value
as obtained previously for 20 msec--both are two "ticks"
of the 60 Hz RTC that's the underlying timer of PAUSE

Here's a link that discusses the details of high res timers
in the PC architecture pretty clearly. If you need better timing
resolution, you'll have to implement it via mex in Matlab. There
are several C/C++ implementations floating around; I've not had need
recently so don't actually have anything specific on hand at
the moment that I can unequivocally recommend. You might do a
search on the File Exchange and see if somebody has posted an
implementation there already.

<http://www.luxford.com/high-performance-windows-timers>

--

Subject: how to make a delay in a "for" cycle?

From: Gabriel Ortega Rosero

Date: 8 Apr, 2013 01:42:10

Message: 9 of 11

dpb <none@non.net> wrote in message <kjt58j$4l2$1@speranza.aioe.org>...
> On 4/7/2013 4:23 PM, dpb wrote:
> > On 4/7/2013 3:52 PM, Gabriel Ortega Rosero wrote:
> >> dpb <none@non.net> wrote in message <kjsgvl$4r1$1@speranza.aioe.org>...
> >>> On 4/7/2013 2:04 PM, Gabriel Ortega Rosero wrote:
> >>> > dpb <none@non.net> wrote in message
> >>> <kjs87h$v7i$1@speranza.aioe.org>...
> >>> >> On 4/7/2013 11:10 AM, Gabriel Ortega Rosero wrote:
> >>> >> > Hi, I need to do matlab data acquisition, but I need control the
> >>> rate
> >>> >> > and amount of data stored in a vector. I made a "for cycle", but
> >>> I need
> >>> >> > that the cycle takes place each unit time defined, up to the total
> >>> >> > acquisition time. I mean, if t=1, I need 500 samples stored in a
> >>> >> second,
> >>> >> > one each 0.002ms. How I can do that?. Thanks!
> >>> >> >
> >>> >> ...
> >>> >>
> >>> >> > s = serial('COM3');
> >>> >> > set(s,'BaudRate',9600);
> >>> >> ...
> >>> >>
> >>> >> Well, at 9600 baud if the data stream is 10 characters/value it will
> >>> >> be about 100 values/sec maximum thruput w/ no overhead so you don't
> >>> >> need to wait but to hurry up.
> >>
> >> The device is a arduino one, do you know it? and the code inside it is
> >> this:
> >>
> >> int out1 = 0;
> >> void setup() {
> >> // begin serial port
> >> Serial.begin(9600);}
> >>
> >> void loop() {
> >> // read pin
> >> out1 = analogRead(A0);
> >> // send
> >> Serial.println(out1);
> >> // wait
> >> delay(1);}
> >> The time delay is in ms, then de devie is reading and sending data at
> >> 1Khz, is that correct?
> >
> > No, heard of it but I don't know it otomh; you got a web url?
> >
> > And, yes, while the resolution of the argument in C library delay
> > function is in msec, that doesn't mean the PC RTC resolution will be
> > anything close to that; only that the delay will be at least 1 msec.
> > More likely it will be about 10.
> >
> > Plus, it also only waits from the time the time the code in
> >
> > out1=analogRead(A0);
> > Serial.println(out1);
> >
> > has executed until the next beginning of the call to the next
> > analogRead() call; how long it takes for that to happen is dependent on
> > the amount of data being transmitted in the message and the overhead in
> > the rest of the handshaking w/ the device and so on and so forth...
> >
> > The above is purely a round-robin loop that essentially runs at whatever
> > speed it can but is highly unlikely imo to be close to 500 Hz if the
> > data message is of any length at all and the timer is implemented in the
> > C library w/ the HS timer and much other luck going on...
> >
> > What is the format of the data that you expect?
>
> OK, I looked up the Arduino analogRead() docs...it returns only a single
> word holding the 10-bit A/D 0-1023 so there isn't a conversion to scaled
> values going on that I presumed earlier. That speeds things up quite a
> bit...they claim ~10 kHz but that is essentially 1/9600 so there's no
> margin there at all.
>
> What you can get in ML w/ the tight loop you'll just have to find out
> experimentally.
>
> How precise timing do you need? What are you going to be doing w/ the
> data? If it's for something like spectral analysis or such where
> precise sampling frequencies are critical it's not going to be
> consistent enough between samples I'd expect even if you get
> approximately the desired average sampling rate. If it's simply to
> observe a phenomenon at roughly 500 Hz, then mayhaps you'll be
> ok--assuming nothing is done to cause the PC to task context shift,
> etc., etc, etc., while sampling.
>
> I think the only way you'll be able to do what your original question
> asked will be by trial timings to see how fast it runs and then figure
> out what you need as a delay, if any.
>
> Here is an example here of what happens w/ the "ordinary" RTC in a PC
> and the PAUSE function in ML that relies on it...
>
> >> % try 2 msec pause...
> >> while 1,tic;pause(0.002),toc,end
> Elapsed time is 0.024567 seconds.
> Elapsed time is 0.005751 seconds.
> Elapsed time is 0.015492 seconds.
> Elapsed time is 0.015530 seconds.
> Elapsed time is 0.015288 seconds.
> ...
> Elapsed time is 0.015439 seconds.
> Elapsed time is 0.015503 seconds.
> Elapsed time is 0.015832 seconds.
>
> NB: the average result is that of the 60 Hz RTC...
>
> % A 10 msec gives same result....
> >> while 1,tic;pause(0.01),toc,end
> Elapsed time is 0.004638 seconds.
> Elapsed time is 0.014507 seconds.
> Elapsed time is 0.015291 seconds.
> Elapsed time is 0.015596 seconds.
> ...
> Elapsed time is 0.015743 seconds.
> Elapsed time is 0.015338 seconds.
>
> Only see a change when > that resolution...
> Try 20 mse>16.66...
>
> >> while 1,tic;pause(0.02),toc,end
> Elapsed time is 0.021175 seconds.
> Elapsed time is 0.030344 seconds.
> Elapsed time is 0.031068 seconds.
> ...
> Elapsed time is 0.030784 seconds.
> Elapsed time is 0.031173 seconds.
> Elapsed time is 0.031260 seconds.
>
> And, to see the granularity, we'll
> try 30 < 2*16.666....
>
> >> while 1,tic;pause(0.03),toc,end
> Elapsed time is 0.020534 seconds.
> Elapsed time is 0.030406 seconds.
> Elapsed time is 0.031087 seconds.
> Elapsed time is 0.030940 seconds.
> Elapsed time is 0.031060 seconds.
> Elapsed time is 0.031249 seconds.
> Elapsed time is 0.031412 seconds.
> ...
> Elapsed time is 0.030828 seconds.
> Elapsed time is 0.030954 seconds.
> Elapsed time is 0.031778 seconds.
>
> So we get roughly 30 msec but it's the same value
> as obtained previously for 20 msec--both are two "ticks"
> of the 60 Hz RTC that's the underlying timer of PAUSE
>
> Here's a link that discusses the details of high res timers
> in the PC architecture pretty clearly. If you need better timing
> resolution, you'll have to implement it via mex in Matlab. There
> are several C/C++ implementations floating around; I've not had need
> recently so don't actually have anything specific on hand at
> the moment that I can unequivocally recommend. You might do a
> search on the File Exchange and see if somebody has posted an
> implementation there already.
>
> <http://www.luxford.com/high-performance-windows-timers>
>
Thanks you so much! The signal is a EMG signal, and have a range between 20 -300Khz.... I found another way to reach my objective with a specific harwarde support package in matlab,this allow me obtain the signal in real time through simulink. I hope it works

Subject: how to make a delay in a "for" cycle?

From: dpb

Date: 8 Apr, 2013 02:10:50

Message: 10 of 11

On 4/7/2013 8:42 PM, Gabriel Ortega Rosero wrote:
...

> Thanks you so much! The signal is a EMG signal, and have a range between
> 20 -300Khz.... I found another way to reach my objective with a specific
> harwarde support package in matlab,this allow me obtain the signal in
> real time through simulink. I hope it works

I'd say for that purpose and w/ that bandwidth you'll definitely need
dedicated hardware; probably w/ onboard buffering.

Also remember that for Nyquist you'll have to sample at >2*Fmax of
actual real energy content in the underlying signal. So if the above
300 kHz is the actual then you'll need a board that can sample at 666
kHz or thereabouts as a bare minimum.

--

Subject: how to make a delay in a "for" cycle?

From: Yair Altman

Date: 8 Apr, 2013 06:44:10

Message: 11 of 11

You might find the following related articles illuminating:

http://UndocumentedMatlab.com/blog/pause-for-the-better/
http://UndocumentedMatlab.com/blog/waiting-for-asynchronous-events/

Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us