using set to plot within a for loop, having trouble with setting up multiple plots (set title, subplots etc.)
16 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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.
More Answers (1)
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
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);
See Also
Categories
Find more on Specifying Target for Graphics Output 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!