Real time NI-DAQ data plot in Matlab with animatedline.
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I tested this code and it works, but I would like to change it so that the time in seconds appears on the x-axis and not datetime. More precisely, I want to set the time limits of the x-axis to be between 0 and 10 seconds and then stop aquisition, so that I can display the signal from ni daq usb 6001. I also want that at the end of the acquisition, the entire signal remains displayed in the graph. Please, if possible, answer me.
As the code shows, I can read the data, live, but at the end of the 10 seconds the graph disappears, but I want it not to disappear. Once again, I want the x-axis to display the time in seconds and to remain stable (axis does not move, only the signal).
clear
close all
time = 0;
data = 0;
`% Set up the plot`
figure(1)
plotGraph = plot(time,data,'-r' );
title('DAQ data log','FontSize',15);
xlabel ('Elapsed Time (s)','FontSize',10)
ylabel('Voltage (V)','FontSize',10);
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.XGrid = 'on';
% Set up the data acquisition
dq = daq("ni");
ch1 = addinput(dq, "Dev1", "ai0", "Voltage");
dq.Rate = 1000;
% Start the data acquisition
start(dq, "Duration", seconds(10))
n = ceil(dq.Rate/10);
while ishandle(plotGraph)
data = read(dq, n);
voltage = data.Dev1_ai0;
t = datetime('now');
for i = 1:100
% Add points to animated line
if isvalid(h)
addpoints(h, datenum(t), voltage(i))
end
end
% Update axes
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
disp('Plot Closed')
2 commentaires
Umar
le 29 Juil 2024
Modifié(e) : Walter Roberson
le 29 Juil 2024
Hi @TARAN OVIDIU ,
So, your goal is to set the x-axis limits between 0 and 10 seconds, stop data acquisition after 10 seconds, and keep the entire signal displayed on the graph without it disappearing. To achieve this task, I made modifications to your code. First,
Setting X-Axis Limits and Plot Stability:
* To set the x-axis limits between 0 and 10 seconds, we need to adjust the x-axis limits of the plot.
* To ensure the plot remains stable after 10 seconds, we will stop the data acquisition loop and keep the entire signal displayed on the graph.
Now, moving to code modifications part:
* Modify the code snippet where the x-axis limits are set to ensure it ranges from 0 to 10 seconds.
* Update the data acquisition loop to stop after 10 seconds and keep the entire signal displayed on the graph.
Here is your modified code,
clear
close all
time = 0;
data = 0;
% Set up the plot
figure(1)
plotGraph = plot(time, data, '-r');
title('DAQ data log', 'FontSize', 15);
xlabel('Elapsed Time (s)', 'FontSize', 10);
ylabel('Voltage (V)', 'FontSize', 10);
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.XGrid = 'on';
% Set up the data acquisition
dq = daq("ni");
ch1 = addinput(dq, "Dev1", "ai0", "Voltage");
dq.Rate = 1000;
% Start the data acquisition
start(dq, "Duration", seconds(10))
n = ceil(dq.Rate / 10);
while ishandle(plotGraph) && seconds(datetime('now') - datetime(dq.InitialTriggerTime)) <= 10
data = read(dq, n);
voltage = data.Dev1_ai0;
t = datetime('now');
% Add points to animated line
addpoints(h, seconds(t - dq.InitialTriggerTime), voltage)
% Update axes
ax.XLim = [0, 10];
drawnow
end
disp('Data Acquisition Completed')
Feel free to adjust the code further based on your specific requirements. Hope this answers your question.
Mario Malic
le 29 Juil 2024
Check my app, it's decent and has some bugs, but it might help.
Réponses (1)
Sandeep Mishra
le 3 Sep 2024
Hi Taran,
I observe that you are working on modifying the x-axis of your plot from a 'datetime' range to a 'double' range spanning from 0 to 10, while ensuring the axis remains stable throughout the plotting process.
To implement the functionalities, you can follow the below approach.
- To maintain a stable axis, set fixed limits for both the x and y axes. You can set the x-limit to [0, 10] and the y-limit to [-1, 10] according to your requirements by using the ‘xlim’ and ‘ylim’ functions. This eliminates the need to repeatedly set the ‘xlim’ within the loop, so you can comment out that part of code.
- To change the x-axis from ‘datetime’ range to ‘double’ range, initialize a variable to record the start time using the ‘datetime’ function before entering the loop. Then, initialize the start time within the loop.
- For appending new data to the plot, calculate the time difference between the current time and the start time.
- To exit the loop when no more data is received, check if the data is empty and break the loop accordingly.
You can refer the following modified code snippet:
time = 0;
data = 0;
% Set up the plot`
figure(1)
plotGraph = plot(time,data,'-r' );
title('DAQ data log','FontSize',15);
xlabel ('Elapsed Time (s)','FontSize',10)
ylabel('Voltage (V)','FontSize',10);
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.XGrid = 'on';
% Set up the data acquisition
dq = daq("ni");
ch1 = addinput(dq, "Dev1", "ai0", "Voltage");
dq.Rate = 1000;
% Start the data acquisition
start(dq, "Duration", seconds(10))
n = ceil(dq.Rate/10);
% Change the axis limit using xlim and ylim
ylim([-1 10]);
xlim([0 10]);
% Create variable to record the start time
t_start = datetime.empty;
while ishandle(plotGraph)
data = read(dq, n);
t = datetime('now');
% Prevent plot crash when data is empty and break the loop
if isempty(data)
break;
end
% Initialise variable to record the start time
if isempty(t_start)
t_start = datetime('now');
end
voltage = data.Dev1_ai0;
for i = 1:100
% Add points to animated line
if isvalid(h)
% Update addpoints method to change the x-axis limits
addpoints(h, seconds(datetime('now') - t_start), voltage(i));
end
end
drawnow
end
disp('Plot Closed')
Please refer to the following documentation for more information.
- ‘xlim’ function: https://www.mathworks.com/help/releases/R2021a/matlab/ref/xlim.html
- ‘ylim’ function: https://www.mathworks.com/help/releases/R2021a/matlab/ref/ylim.html
- ‘datetime’ function: https://www.mathworks.com/help/releases/R2021a/matlab/ref/datetime.html
- ‘seconds’ function: https://www.mathworks.com/help/releases/R2021a/matlab/ref/duration.seconds.html
I hope this helps.
0 commentaires
Voir également
Catégories
En savoir plus sur Data Acquisition Toolbox Supported Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!