using set to plot within a for loop, having trouble with setting up multiple plots (set title, subplots etc.)
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
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 comentarios
Andre
el 16 de Jun. de 2023
Gourab
el 16 de Jun. de 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);
Categorías
Más información sobre Graphics Performance en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!