Clear Filters
Clear Filters

USB-2416-4AO Matlab Live user interface

21 views (last 30 days)
Harshaun Mann
Harshaun Mann on 23 May 2024
Answered: Nivedita on 27 May 2024 at 21:02
I want to make a code that takes in analog inputs from a DAQ and is able to store that data in plots as well as send output signals based on the inputs. I cannot seem to get the code to store any data or read data directly from the DAQ in matlab.
My code so far:
d = daqlist("mcc");
d(1, :)
dq = daq("mcc")
addinput(dq, "Board0", "Ai0", "Voltage");
dq
[data, startTime] = read(dq, seconds(1));
plot(data.Time, data.Board0_Ai0);
xlabel("Time (s)");
ylabel("Voltage (V)");
  1 Comment
Harshaun Mann
Harshaun Mann on 23 May 2024
I get this error Warning: DAQSDK Warning: Ignored call to method 'stop' in state 'ReadyState'.

Sign in to comment.

Answers (1)

Nivedita
Nivedita on 27 May 2024 at 21:02
Hello Harshaun,
Your code seems to be correct for reading data from a DAQ device and plotting it. However, if you’re not able to read or store any data, there could be a few reasons for this:
  1. Check that the DAQ device is properly connected to your computer and is recognized by MATLAB. You can do this by running daqlist in the MATLAB command window. This should list all the connected DAQ devices.
  2. Make sure that the channel (“Ai0”) and the measurement type (“Voltage”) you’re trying to add with addinput are correct and supported by your DAQ device.
  3. The read function reads data from the DAQ for a specified duration (1 second in your case). If the DAQ is not generating or receiving any data during this time, the read function will not return any data.
  4. Ensure that the data returned by the read function contains the fields Time and Board0_Ai0. If these fields are not present in the data, the plot function will not be able to plot the data.
You can modify your code in the following manner to include some error checking:
d = daqlist("mcc");
if isempty(d)
error('No MCC devices found')
end
dq = daq("mcc");
if isempty(dq)
error('Failed to create MCC DAQ session')
end
try
addinput(dq, "Board0", "Ai0", "Voltage");
catch ME
error('Failed to add input channel: %s', ME.message)
end
try
[data, startTime] = read(dq, seconds(1));
catch ME
error('Failed to read data: %s', ME.message)
end
if ~isfield(data, 'Time') || ~isfield(data, 'Board0_Ai0')
error('Data does not contain expected fields')
end
plot(data.Time, data.Board0_Ai0);
xlabel("Time (s)");
ylabel("Voltage (V)");
As for the error message that you are facing, it is typically due to a call to the ‘stop’ method when the Data Acquisition (DAQ) object is not running. Here are a few things you could try for that:
  1. You can use the isrunning function to check if the DAQ is running before attempting to stop it. If it’s running, then you can stop it; otherwise, you can skip the stop command.
  2. You can use the start function before your read command and the stop function after your read command. This ensures that the DAQ is running when you’re reading data and is stopped afterwards.
  3. Sometimes, specifying the output format and the number of samples to read at a time can help avoid issues. For example, you can try something like this:
dq.Rate = 8000; %replace 8000 with your desired sampling rate
dq.DurationInSeconds = 1;
start(dq);
[data, time] = read(dq, dq.DurationInSeconds * dq.Rate, "OutputFormat", "Matrix");
stop(dq);
plot(time, data);
I hope these suggestions will help you identify the issue and solve your problem!

Categories

Find more on Data Acquisition Toolbox Supported Hardware in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!