Stuart’s MATLAB Videos

Watch and Learn

Advanced MATLAB: Data acquisition in MATLAB 25

Posted by Doug Hull,

MATLAB is often used for post-test data analysis. You can get more value from your data acquisition session if you do the MATLAB analysis on the data as it is being collected. This can be done by having MATLAB be the software for collecting your data. This video shows a very simple way of getting data into MATLAB from a DAQ board, in this case the windows soundcard. This demo uses the Data Acquisition Toolbox.
Other videos have been gathered here: Other Advanced MATLAB posts have been gathered here:

25 CommentsOldest to Newest

Juan Manuel Merlo replied on : 1 of 25

Hi Doug, I saw your demonstrations of, but I can’t see the winsound input, matlab said me the following:
??? Error using ==> analoginput.analoginput
Error using ==> analoginput.analoginput>localCreateAnalogInputObject
Failure to find requested data acquisition device: adaptor.
Can you help me please?
Juan Manuel “El Mer” Merlo

Di-An Hong replied on : 3 of 25

My questions are
How to use Matlab to record computer mouse movement
Do I need the Data Acquistion Toolbox to do so


Dayal Strub replied on : 5 of 25

Hi Doug, I have a question regarding DAT and usb mice, which doesn’t fit under either of your two previous posts.

I am trying to devise a cheap pendula experiment for university and highschool students,
the idea is to use a usb mouse (as has been done before, but differently and not using Matlab) as the sensor. I would like to use Matlab to acquire data from the usb mouse (specificaly the coordinates which would now be angles), but it should not be identified as a mouse, and should not interfere with the computers primary one.
I thought it might be possible to use DAT by creating an adaptor for the usb port, and then considering considering it as a digital input and not as a mouse.

As I have never used DAT before I am not sure this is possible, and if it is, exactly how to go about it.
Any help would therefore be very much apreciated!


Bob replied on : 6 of 25

Dayal, here’s another thought that could use the Data Acquisition Toolbox. Use a rotary potentiometer (variable resistor) as a sensor. The basic idea here is voltage divider. Place a reference voltage (say 5V) across the ends and connect the wiper pin to the analog input channel. The digitized voltage would be proportional to the rotation angle of the pot. Many cars now use exactly this for the gas pedal sensor, In fact, maybe you can take the foot pedal off a video game, or from a sewing machine, etc.


Gert replied on : 7 of 25

Hi Doug,
Is there a way to analyze the rpm of an engine from the sound with Matlab? I downloaded an example “Real time octave analysis” and I tried to put in a band-pass filter but the program crashes every time when I try to run it.
If there is a way to do that, can you give me an idea how? For an 12 cylinder 4stroke engine and rpm between 800 – 9000 (16.67-150 Hz) it gives a maximum of 900 pulses a second. So I think I will need than a Fs > 1800 Hz. But is there a way to filter real time data ?


Doug replied on : 8 of 25


I can not speak to the stability of any of the File Exchange, you will need to speak to the original author about that. However, to filter “Real Time Data”, you need to be more specific. Are you tying to get the results of your analysis while the test is running, or is this post test analysis. If you want the results while the test is running, how “Real Time” do you need to be. You might end up using Real Time Workshop. Explain more what you are looking for, and I will try to help.


Bryan replied on : 9 of 25

Hi Doug –

I’m trying to use an accelerometer to measure the tilt angle and record that data in MATLAB. I have it hooked up to a power supply and to a Measurement Computer USB-1208FS DAQ. Then I have code in MATLAB to acquire the data. It works for a second or two, but then it stops. Is there a template code for doing something like this? Any clue on the problem?

– Bryan

pierre replied on : 10 of 25

Hi all,
I have the Data Acquisition Toolbox, and I’m trying for 2 weeks to send a step voltage, and just after another one of a different voltage WITHOUT going back to “zero” between the two..
I tried to do it little by little and I saw that the “start” function between the two steps reinitialize the voltage at a value of zero volts. And I wouldn’t like it because the piezo I want to control has a really fast response time..

Furthermore I think I am compelled to use a “start” function between the two steps because I have to compute de Amplitude of the second step in real time so I don’t have to prepare the two steps before and put both of them in the engine using one “putdata”.

Is there a way to avoid it ?
Thank you a lot, This problem is really annoying me.


Sherryl replied on : 11 of 25

Hello Pierre,

Please look at the OutOfDataMode property.

You can use it to set a value that will be held when the queued data has been output.

start will not set the output voltage to zero until that value is in your output signal.


Sherryl replied on : 12 of 25

In Response to Post #10 by Bryan –
Hi Bryan,
By default the analog input object will acquire one second worth of data.
There are many ways to acquire more than that.
1. Set TriggerRepeat to greater than 0
2. Set SamplesPerTrigger to a larger number than the default, which matches the default SampleRate
3. Use either a TimerFcn or SamplesAcquiredFcn callback to restart the acquisition

All of these things are well documented here:
They can also be seen in the demos . Bring up MATLAB and type:
demo(‘toolbox’,’data acquisition’)
at the command line (requires that Data Acquisition Toolbox already is installed).


pierre replied on : 13 of 25

Hi sherryl and thank you for answering me,

Actually, I already tried before to use this property because I saw that it was what I wanted, nevertheless I got this error:

EDU>> AO = analogoutput(‘ueidaq’,’pwrdaq://dev0′);
EDU>> addchannel (AO, 0);
EDU>> AO.OutOfDataMode = ‘DefaultValue’;
??? Error using ==> daqdevice.subsasgn at 108
Error using ==> daqdevice.set at 261
Invalid property: ‘OutOfDataMode’.

Then I tried to skip it just by doing :
EDU>> AO.Channel.DefaultChannelValue = 5;

And then I had no error but also no effect : I still got a value of “zero” instead of 5 volts between the two steps ..

I don’t know why this property is not recongnized, do you ?
I am using the R2007a student version of matlab.


Gamal replied on : 14 of 25

Dear Sir,
I am working in a project that i use an Analogue to digital converter HW to transfer analogue electrical data to digital. I want to read this digital these data via Matlab using USB port in order to get its FFT. I would be very glad if you tell me how to do that. thanks alot for your help.

Doug replied on : 15 of 25


That is something that can be done. Do you have the Data Acquisition toolbox? What have you tried, can you connect to the board? What board is it?


Gamal replied on : 16 of 25

Thank u Doug for ur fast reply. yes I have an NI-DAQ USB 6008. and I have an Analogue voltage sinusoidal signal that have a maximum frequency of 500KHz which means i want a minimum sampling of 1 Mhz. I want to analyze this signal using matlab( getting FFT) real time. can this be done by using the ADC in NI-DAQ or i have to get another ADC. if so how can i interface it with matlab. thanks alot Doug and I am waiting for your reply.

Javier replied on : 17 of 25

i’d like to know how can i import data for a transducer of human pulses into matlab without help of other hardware. the sensor has a USB terminal?

Pirat replied on : 18 of 25

hi doug. i want to ask you about using data acquisition card to record data from an oscilloscope realtime and process it with Matlab. i’m able to use getdata, but it’s blocking since i want real time recording and processing, then i will write a GUI to calculate the period, amplitude and some other things.Thank you

Sal22 replied on : 20 of 25

Greetings All

I have a question on how I can prevent clicks/pause while trying to
play a looped audio file (created in matlab) continuously.

The working example below plays a 1 second 100hz signal 5 times but I
can’t figure out a way to make it continuous so it doesn’t start and
stop with that pause/click. I tried to increase the array to like 30
seconds which would work but I want to run the loop for an hour or
three. When I tried this I ran out of memory (the array was just to
large to store in my 32bit 2gig ram windows system) Any suggestions/help

I also would like to be able to change the frequency (nSigRepeat) using a slider in a GUI and have it update / play the audio in realtime are their any video tutorials on this

nSigRepeat=100; %how many times signal repeats in a second
nTimeIncreaseTo=1; %Duration of signal in seconds
y=sin(2*pi*x*nSigRepeat)*.8; %adjust amplitude by multiplying to
prevent clipping



%Plays audio file for 1 second 5 times at 100 hz.
for i=1:5

tia sal22
and keep up the great work

dhull replied on : 21 of 25


I think that you might be able to get audioplayer to do what you want. I wonder if the click you are getting is because of a discontinuity between the end and beginning of the loop.

If you have Data Acquisition Toolbox, then you might be able to create and analog output and push data out that way and avoid the discontinuity problem also.

What kind of audio data are you attempting this with?


Guillaume Defrance replied on : 22 of 25

Hi Doug,

I have a question about how to use a National Instruments PCI 6120 card with Matlab. The sampling frequency can go up to 1MHz, as far as I undesrtand from the specifications. However, with MatLab, I can’t set it greater than 500kHz.

set(ai,'SampleRate', Fe); % this is my input line
% Here is the Matlab answer
??? Error using ==> daqdevice/set
Unable to set SampleRate above maximum value of 500000.

Do you or anybody have heard about this problem? I am pretty sure there exists a way to force the NI card to a larger sampling frequency when it is given in the specifications of the card, maybe by changing some properties of the card using Matlab?
Thank you in advance for your answer.

— Guillaume

Amin replied on : 23 of 25

i m working on a project, i must control my PCI Data Acquisition Card (Keithley KPCI-3103A) and make it send (with matlab commands) Pulses through a Digital Output to a Sensor board.
These Signals must be at least 13µs High and 13µs Low (with adjustable pulses Number, Duration and Pulse Width) i looked maybe the whole Documentation about DAQ Toolbox and another Privat things, but nothing of that helped me. I m really confused.
I can get the Analog signal from the Sensor Board (Adaptor is already registred) but when it´s not controlled with the mentioned signal it is just a normal Input Signal.

I hope somebody can help

Navin Kumar replied on : 25 of 25

Hi Doug, I am doing my final year project in which I need your help. My project is based on real time system(Level sensor). I have to control the level of the water in the tank using microcontroller and MATLAB. Till now I have already done the microcontroller part but, I am stuck with MATLAB GUI. I need your help to show the data in MATLAB GUI while the test is running. I have used ATMega32 microcontroller and ZigBee Transceiver Module.

Add A Comment

What is 1 + 6?

Preview: hide