Azzera filtri
Azzera filtri

How to create multiple live updating plots on one script?

4 visualizzazioni (ultimi 30 giorni)
We have flowrate, temperature, and pressure sensors that record data and send it to an arduino which then is connected to matlab on our computer. We were able to get live updates of just the flow sensor, but we want to create 3 seperate windows to display all three types of data at once. Here is our code so far
clear
a = arduino; %connecting to arduino
flowrate_archive_a = [];%creating an empty arrary for the final array of flowrate
flowrate_archive_b = [];
flowrate_archive_c = [];
flowrate_archive_d = [];
flowrate_archive_e = [];
flowrate_archive_f = [];
i=1; %counter variable
%pump_count = input("How many Flow Rate Monitors are being Run? Ex:4");
%fprintf("Make sure Pump A is Wired to A0, Pump B into A1, and so on ***Leave every pump plugged in even if not in use***")
%pump_order = input('What order are the pumps being used? Ex: A,C,E,F')
%ax_1 = axes(); %creating the graph
% hold on; %making the graph stay for the entire lenth of run
ylim([0,11])%Setting y Limit
title("Flowrate (5min)")%setting the title
xlabel("Time (s)") %labeling the x axis
ylabel("Flowrate (mL/min)")%labeling the y axis
legend('Pump A','Pump B', 'Pump C', 'Pump D', 'Pump E', 'Pump F')
if readVoltage(a,'A0') ~= 0
voltage_a = readVoltage(a,'A0');%reading voltage from arduino and storing it as a variable
flowrate_archive_a(i) = (voltage_a-0.8077)/0.2876; %converting the voltage to flowrate
line1 = line(i,flowrate_archive_a(i),'Color','red'); %Creating the line for flowrate A
else
line1 = line(i,0)
end
if readVoltage(a, 'A1') > 0.5
voltage_b = readVoltage(a,'A1');%reading voltage from arduino and storing it as a variable
flowrate_archive_b(i) = (voltage_b-0.8077)/0.2876 %converting the voltage to flowrate
line2 = line(i,flowrate_archive_b(i),'Color','#D95319'); %Creating the line for flowrate B
else
line2 = line(i,0)
end
if readVoltage(a, 'A2') ~= 0
voltage_c = readVoltage(a,'A2');%reading voltage from arduino and storing it as a variable
flowrate_archive_c(i) = (voltage_c-0.8077)/0.2876; %converting the voltage to flowrate
line3 = line(i,flowrate_archive_c(i),'Color','#EDB120'); %Creating the line for flowrate C
else
line3 = line(i,0)
end
if readVoltage(a, 'A3') ~= 0
voltage_d = readVoltage(a,'A3');%reading voltage from arduino and storing it as a variable
flowrate_archive_d(i) = (voltage_d-0.8077)/0.2876; %converting the voltage to flowrate
line4 = line(i,flowrate_archive_d(i),'color',"#77AC30"); %Creating the line for flowrate D
else
line4 = line(i,0)
end
if readVoltage(a, 'A4') ~= 0
voltage_e = readVoltage(a,'A4');%reading voltage from arduino and storing it as a variable
flowrate_archive_e(i) = (voltage_e-0.8077)/0.2876; %converting the voltage to flowrate
line5 = line(i,flowrate_archive_e(i),'color','#4DBEEE'); %Creating the line for flowrate E
else
line5 = line(i,0)
end
if readVoltage(a, 'A5') ~= 0
voltage_f = readVoltage(a,'A5');%reading voltage from arduino and storing it as a variable
flowrate_archive_f(i) = (voltage_f-0.8077)/0.2876; %converting the voltage to flowrate
line6 = line(i,flowrate_archive_f(i),'color','m'); %Creating the line for flowrate F
else
line6 = line(i,0)
end
%line2 = line(i,flowrate_archive_b)
i=i+1;%adding to the counter
while(1)
voltage_a = readVoltage(a,'A0');
flowrate_archive_a(i) = (voltage_a-0.8077)/0.2876;
voltage_b = readVoltage(a,'A1');
flowrate_archive_b(i) = (voltage_b-0.8077)/0.2876;
voltage_c = readVoltage(a,'A2');
flowrate_archive_c(i) = (voltage_c-0.8077)/0.2876;
voltage_d = readVoltage(a,'A3');
flowrate_archive_d(i) = (voltage_d-0.8077)/0.2876;
voltage_e = readVoltage(a,'A4');
flowrate_archive_e(i) = (voltage_e-0.8077)/0.2876;
voltage_f = readVoltage(a,'A5');
flowrate_archive_f(i) = (voltage_f-0.8077)/0.2876;
pause(1.0);%waiting for 1 second between samples
if(i<300) %setting max X axis length
line1.XData = [line1.XData i];
line2.XData = [line2.XData i];
line3.XData = [line3.XData i];
line4.XData = [line4.XData i];
line5.XData = [line5.XData i];
line6.XData = [line6.XData i];
else
xlim([i-299,i]) %Limiting x axis length to 300 sec
line1.XData = [line1.XData i];
line2.XData = [line2.XData i];
line3.XData = [line3.XData i];
line4.XData = [line4.XData i];
line5.XData = [line5.XData i];
line6.XData = [line6.XData i];
end
line1.YData = [line1.YData flowrate_archive_a(i)];
line2.YData = [line2.YData flowrate_archive_b(i)];
line3.YData = [line3.YData flowrate_archive_c(i)];
line4.YData = [line4.YData flowrate_archive_d(i)];
line5.YData = [line5.YData flowrate_archive_e(i)];
line6.YData = [line6.YData flowrate_archive_f(i)];
i=i+1;
end

Risposta accettata

Andrew
Andrew il 23 Mag 2024
clear;
% Connect to Arduino
a = arduino;
% Initialize flowrate archives
flowrate_archive_a = [];
flowrate_archive_b = [];
flowrate_archive_c = [];
flowrate_archive_d = [];
flowrate_archive_e = [];
flowrate_archive_f = [];
i = 1; % Counter variable
% Create two figures for separate plots
figure1 = figure('Name', 'Flow Rate Monitor A, B, C');
figure2 = figure('Name', 'Flow Rate Monitor D, E, F');
% Set up the first figure with subplots for A, B, C
subplot(3, 1, 1, 'Parent', figure1);
line1 = animatedline('Color', 'red');
title('Flowrate A (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax1 = gca;
subplot(3, 1, 2, 'Parent', figure1);
line2 = animatedline('Color', '#D95319');
title('Flowrate B (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax2 = gca;
subplot(3, 1, 3, 'Parent', figure1);
line3 = animatedline('Color', '#EDB120');
title('Flowrate C (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax3 = gca;
% Set up the second figure with subplots for D, E, F
figure(figure2);
subplot(3, 1, 1, 'Parent', figure2);
line4 = animatedline('Color', '#77AC30');
title('Flowrate D (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax4 = gca;
subplot(3, 1, 2, 'Parent', figure2);
line5 = animatedline('Color', '#4DBEEE');
title('Flowrate E (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax5 = gca;
subplot(3, 1, 3, 'Parent', figure2);
line6 = animatedline('Color', 'm');
title('Flowrate F (5min)');
xlabel('Time (s)');
ylabel('Flowrate (mL/min)');
ylim([0, 11]);
ax6 = gca;
% Main loop to update the plots
while true
% Read voltages from Arduino
voltage_a = readVoltage(a, 'A0');
voltage_b = readVoltage(a, 'A1');
voltage_c = readVoltage(a, 'A2');
voltage_d = readVoltage(a, 'A3');
voltage_e = readVoltage(a, 'A4');
voltage_f = readVoltage(a, 'A5');
% Convert voltages to flowrates
flowrate_a = (voltage_a - 0.8077) / 0.2876;
flowrate_b = (voltage_b - 0.8077) / 0.2876;
flowrate_c = (voltage_c - 0.8077) / 0.2876;
flowrate_d = (voltage_d - 0.8077) / 0.2876;
flowrate_e = (voltage_e - 0.8077) / 0.2876;
flowrate_f = (voltage_f - 0.8077) / 0.2876;
% Append to archives
flowrate_archive_a = [flowrate_archive_a flowrate_a];
flowrate_archive_b = [flowrate_archive_b flowrate_b];
flowrate_archive_c = [flowrate_archive_c flowrate_c];
flowrate_archive_d = [flowrate_archive_d flowrate_d];
flowrate_archive_e = [flowrate_archive_e flowrate_e];
flowrate_archive_f = [flowrate_archive_f flowrate_f];
% Update the lines with new data
addpoints(line1, i, flowrate_a);
addpoints(line2, i, flowrate_b);
addpoints(line3, i, flowrate_c);
addpoints(line4, i, flowrate_d);
addpoints(line5, i, flowrate_e);
addpoints(line6, i, flowrate_f);
% Draw updates
drawnow;
% Pause for 1 second between updates
pause(1);
% Increment counter
i = i + 1;
% Keep the X axis length to a maximum of 300 points (5 minutes)
if i > 300
set(ax1, 'XLim', [i-300 i]);
set(ax2, 'XLim', [i-300 i]);
set(ax3, 'XLim', [i-300 i]);
set(ax4, 'XLim', [i-300 i]);
set(ax5, 'XLim', [i-300 i]);
set(ax6, 'XLim', [i-300 i]);
else
set(ax1, 'XLim', [0 300]);
set(ax2, 'XLim', [0 300]);
set(ax3, 'XLim', [0 300]);
set(ax4, 'XLim', [0 300]);
set(ax5, 'XLim', [0 300]);
set(ax6, 'XLim', [0 300]);
end
% Optionally, to view the entire graph at the end,
% you could add a condition to break the loop after a certain duration:
% if i >= 28800 % for an 8-hour experiment
% break;
% end
end

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by