Having issue in plotting live data of three channels in a single UI Axes
1 view (last 30 days)
Show older comments
I am having issue in ploting real time data in UI Axes of 3 channels. I am able to plot the data of one channel but when i try for two or three channels it show an error or plots only one channel and the stops in between .Can anyone help me out where i am going wrong or at what thing i am missing to add in my code. The two values i wanna show are trip and alarm line which i want to fetch from app. Plz any one can guide me with the code.
i=1;
ii=1;
while strcmp(app.Switch.Value, 'On')
clear m
m = modbus('serialrtu','COM5',BaudRate=9600);
m.Timeout = 0.275;
% Rack 1 Slot 1 Module 1
serverId1 = 2;
tripM1A = read(m,"holdingregs",1,1,serverId1,"uint16");
alarmM1A = read(m,"holdingregs",6,1,serverId1,"uint16");
fullscaleM1A = read(m,"holdingregs",11,1,serverId1,"uint16");
processM1A = read(m,"holdingregs",44,1,serverId1,"uint16");
% Data of channels
app.ALARM.Value = alarmM1A;
app.TRIP.Value = tripM1A;
app.CHA.Value= processM1A;
% To plot data in UI Axes
data1A(i)=app.CHA.Value;
% data1T(ii)= app.TRIP.Value;
if i<=60
plot(app.UIAxes,data1A,'LineWidth',1.5);
% plot(app.UIAxes,data1T,'LineWidth',1.5);
else
plot(app.UIAxes,data1A(end-60:end),'LineWidth',1.5);
end
title(app.UIAxes,'Live Trend Module 2 Channel A');
xlabel(app.UIAxes,'Time in seconds','Color','k');
ylabel(app.UIAxes,'Amplitude','Color','k')
ylim(app.UIAxes,[0 fullscaleM1A]);
grid(app.UIAxes, "on");
pause(0.1)
i= i+1;
end
0 Comments
Accepted Answer
Pratyush Swain
on 14 Mar 2024
Hi Aditya,
Your current code is structured to plot only one channel of data ("data1A") on your UI Axes. To plot multiple channels including trip and alarm lines, its important to use "hold on" and "hold off" to ensure that all the lines are plotted without erasing the previous ones. Here's a modified version of your code which demonstrates plotting for trip and alarm values.
i = 1;
data1A=[];
data1T=[];
data1Al=[];
while strcmp(app.Switch.Value, 'On')
% Generating random values for trip,alarm & process
tripM1A = 80 + rand(1)*20;
alarmM1A = 50 + rand(1)*10;
processM1A = rand(1)*100;
fullscaleM1A = 100;
% To plot data in UI Axes
data1A(i) = processM1A;
data1T(i) = tripM1A;
data1Al(i) = alarmM1A;
if i <= 60
plotRange = 1:i;
else
plotRange = (i-59):i;
end
% Ensuring previous plots are not erased
hold(app.UIAxes, 'on');
plot(app.UIAxes, plotRange, data1A(plotRange), 'LineWidth', 1.5, 'Color', 'b'); % Process data
plot(app.UIAxes, plotRange, data1T(plotRange), '--', 'LineWidth', 1.5, 'Color', 'r'); % Trip line
plot(app.UIAxes, plotRange, data1Al(plotRange), ':', 'LineWidth', 1.5, 'Color', 'g'); % Alarm line
% Hold off after plotting
hold(app.UIAxes, 'off');
title(app.UIAxes, 'Simulated Live Trend');
xlabel(app.UIAxes, 'Time in seconds', 'Color', 'k');
ylabel(app.UIAxes, 'Amplitude', 'Color', 'k');
ylim(app.UIAxes, [0 fullscaleM1A]);
grid(app.UIAxes, "on");
pause(0.1);
i = i + 1;
end
Please note I have used random data values since I do not have access to Modbus serial connection.Also the "hold" commands need the axes as a parameter. I have also attached the demo app containing the above implemention for your reference.For more information please refer to:
Hope this helps.
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!