Ovidiu Taran
Ovidiu Taran

Reputation: 1

Real time NI-DAQ data plot in Matlab with animatedline

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.

This is the 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)
    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')

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).

Upvotes: 0

Views: 54

Answers (0)

Related Questions