using set to plot within a for loop, having trouble with setting up multiple plots (set title, subplots etc.)

17 views (last 30 days)
hi everyone,
the use for this is to plot "live" data gathered by sensors. i made a test script so i can test animated lines and plot itself, where i just change the values with set instead of using plot the whole time.
the first step was pretty straight forward, even if i did it wrong anyways, wanted to take shortcuts
clc; clearvars; close all;
inputs = 2;
dispdata = 10;
fig{1} = figure();
title('Pa')
fig{2} = figure();
%title('°F')
%fig{3} = figure();
%title('SLpm')
plotaxes{1} = axes('Parent',fig{1});
plotaxes{2} = axes('Parent',fig{2});
%plotaxes{3} = axes('Parent',fig{3});
lineplot{1} = plot(plotaxes{1},0,0);
lineplot{2} = plot(plotaxes{2},0,0);
%lineplot{3} = plot(plotaxes{3},0,0);
datapoints=10;
n=0;
data=zeros(1,inputs);
ddata=zeros(datapoints,inputs);
for k = 1:datapoints
for ii = 1:inputs
rr=randi(10);
data(1,ii)=rr;
end
ddata(k,:)=data;
if k >= dispdata
x = k-dispdata+1:k;
for i = 1:inputs
set(lineplot{i},'XData',x,'YData',ddata(k-dispdata+1:k,i))
xlim([k-dispdata k])
end
else
x = 1:dispdata-k;
for i = 1:inputs
set(lineplot{i},'XData',x,'YData',ddata(1:dispdata-k,i))
xlim([0, dispdata-k]);
end
end
end
so i started with 3 plots, creating for each a figure and then the axes. i then wanted to give them titles and thats already the point i cant get my head around. When i add the title, i get a 2nd x and y axis and i dont know why, or how to do it properly.
it looks like that for all.
after that, i tried to get another input into an existing figure and i always get errors. i either overwrite the existing data, or get an error. like i want for example 3 temperature data in one figure. then i thought about having figures as subplots. this i did see already, seems easy enough, but didnt tried it myself for now.
something else that crossed my mind was setting up colors and other line properties, before i been plot the first data point, that does work with set flawlessly.

Accepted Answer

Prannoy
Prannoy on 16 Jun 2023
Regarding plotting multiple data streams in the same figure, you can create multiple line objects, one for each data stream, within the same axes. Then when you set the x and y data for each line, they will appear in the same plot. Here's an example of how you can do this:
inputs = 3; % increase number of inputs to 3
...
lineplot{1} = plot(plotaxes{1},0,0,'r'); % create a red line for input 1
lineplot{2} = plot(plotaxes{1},0,0,'g'); % create a green line for input 2
lineplot{3} = plot(plotaxes{1},0,0,'b'); % create a blue line for input 3
...
for i = 1:inputs
set(lineplot{i},'XData',x,'YData',ddata(k-dispdata+1:k,i));
xlim([k-dispdata k]);
end
...
Here, we create 3 different line objects, one for each input, and set the color of each line to be red, green, and blue respectively using the plot() function. Then in the loop where we update the plot, we set the x and y data for each line using set() and the correct index. The colors for each individual line can be modified using different plot line specifier arguments.
You should also be able to set line properties such as line width, line style, marker type, etc. using the same set() function with the appropriate property name and value.
  1 Comment
Andre
Andre on 16 Jun 2023
i was a little shocked at first, when i saw the answer, cause the only difference was, i didnt specify a color, aside from that, i did try that. when i do that, the set function throws an error "invalid or deleted object", that led me to believe the lineplot{1} was overwritten, as its not there anymore.
yes, set() worked nicely in regards of changing the line properties. thats also a point that lets me question myself, cause using set() to change the linewidth, color and what not does work perfectly. but anything else does not.

Sign in to comment.

More Answers (1)

Gourab
Gourab on 16 Jun 2023
Edited: Gourab on 16 Jun 2023
Hi Andre,
I understand that you want to know why a third axes is being added when you add title.
When you call the title function on one of your axes, it is adding the second set of axes because the default behavior of title is to create a new set of axes and set the title in there. To avoid this behavior, you need to pass the axis handle as the first argument to title.
Please refer to the below code snippet
clc;
clearvars;
close all;
inputs = 2;
dispdata = 10;
fig{1} = figure();
fig{2} = figure();
fig{3} = figure();
plotaxes{1} = axes('Parent',fig{1});
plotaxes{2} = axes('Parent',fig{2});
plotaxes{3} = axes('Parent',fig{3});
lineplot{1} = plot(plotaxes{1},0,0);
lineplot{2} = plot(plotaxes{2},0,0);
lineplot{3} = plot(plotaxes{3},0,0);
t{1} = annotation(fig{1}, 'textbox', [0.1 0.9 0.8 0.1], 'String', 'Pa', 'FitBoxToText', 'on', 'LineStyle', 'none');
t{2} = annotation(fig{2}, 'textbox', [0.1 0.9 0.8 0.1], 'String', '°F', 'FitBoxToText', 'on', 'LineStyle', 'none');
t{3} = annotation(fig{3}, 'textbox', [0.1 0.9 0.8 0.1], 'String', 'SLpm', 'FitBoxToText', 'on', 'LineStyle', 'none');
datapoints=10;
n=0;
data=zeros(1,inputs);
ddata=zeros(datapoints,inputs);
for k = 1:datapoints
for ii = 1:inputs
rr=randi(10);
data(1,ii)=rr;
end
ddata(k,:)=data;
if k >= dispdata
x = k-dispdata+1:k;
for i = 1:inputs
set(lineplot{i},'XData',x,'YData',ddata(k-dispdata+1:k,i))
xlim([k-dispdata k])
end
else
x = 1:dispdata-k;
for i = 1:inputs
set(lineplot{i},'XData',x,'YData',ddata(1:dispdata-k,i))
xlim([0, dispdata-k]);
end
end
end
I hope this helps you to resolve the query.
  3 Comments
Gourab
Gourab on 16 Jun 2023
Yes, you can use the set() function to combine two plots in MATLAB
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);
% Create two line plots
plot(x, y1, 'r', 'LineWidth', 2); % plot in red
hold on;
plot(x, y2, 'b', 'LineWidth', 2); % plot in blue
% Add legends
legend('sin(x)', 'cos(x)');
% Customize plot appearance using set function
set(ax, 'XGrid', 'on', 'YGrid', 'on', 'GridAlpha', 0.5);
Andre
Andre on 19 Jun 2023
Edited: Andre on 19 Jun 2023
i was a bit suprised about that, cause i was sure i did try that. im aware of hold. the problem is, that this doesnt work in the first example. if i put hold on between the 2nd and 3rd plot, the 3 plots will appear like before and have the data like before, which makes sense, cause they were all explicitly directed to each plot. so i was thinking i have to pass the data at some point to an existing plot, but when i do that, the data gets overwritten, so i cant get 2 data-sets into one plot.
hold would work fine when plot is used normally, like your 2nd example shows, but thats different from the first one. so i ment by using set(), that its used as before to pass the data to the plot, sorry if that wasnt clear.
would using animatedline make more sense ? i wanted to use set() because of speed, but animatedline kinda does the same, only passing the data to the plot, doesnt it ?
the goal are two things, to be able to plot live data for a day each second, but the displayed data is only a fraction for example the last 20 or 100 datapoints. and the 2nd goal is to be able to plot much faster, but then not for such a long time. like plotting every 0.05 seconds maybe for a minute or little longer, to see different, more time critical behavior.

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!