Fixed plot and variable-loop plot

1 次查看(过去 30 天)
Hello everyone,
I need you help. I am trying to generate a plot with fixed lines (vertical ones) and on it, I would like to add a line that varies with the index k of a for loop that goes through a matrix. How can I avoid superimposition of all k-lines generated inside the for loop? I am trying to remove the "hold on" line but the issue is that Matlab removes the vertical lines too.
Thanks in advance for the help.
Here is the code:
assex=[0 25 25 30.7];
figure(1)
set(gcf, 'Position', get(0, 'Screensize'));
ylim([0 100]); yticks(0:2.50:100)
xticks(-1:1:35); xlim([-1 35])
xlabel('Spessore provino (cm)','FontSize',18)
ylabel('Umidità relativa aria (%)','FontSize',18)
set(gca,'FontSize',15)
grid on
xline(assex(1),'k',{'Superficie interna'});
xline(assex(2),'k',{'Freno a vapore - lato interno'},'LabelHorizontalAlignment','left');
xline(assex(3),'k',{'Freno a vapore - lato esterno'});
xline(assex(4),'k',{'Superficie esterna'});
hold on
for k=1:60:length(rh(:,1))
% scatter(assex(1,1),rh(k,1),'red','filled','SizeData',50)
% hold on
% scatter(assex(1,2),rh(k,2),'magenta','filled','SizeData',50)
% hold on
% scatter(assex(1,3),rh(k,3),'green','filled','SizeData',50)
% hold on
% scatter(assex(1,4),rh(k,4),'blue','filled','SizeData',50)
% hold on
% scatter(assex(1,:),rh(k,:),'filled','SizeData',50,'MarkerFaceColor',[0 0.4470 0.7410])
% hold on
plot(assex,rh(k,:),'LineStyle','-','Color',[0 0.4470 0.7410]);
title(string(strcat('Tempo= ',num2str(k/60,'%.2f'),' ore (',num2str(k/(60*24),'%.2f'),') giorni.')))
pause(0.0005)
end
NB: rh is a matrix with n x m dimension (index k goes along n).

采纳的回答

Voss
Voss 2023-7-13
One way would be to create the variable line once before the loop, and update its YData inside the loop.
% some random rh:
rh = 100*rand(1000,4);
assex=[0 25 25 30.7];
figure(1)
set(gcf, 'Position', get(0, 'Screensize'));
ylim([0 100]); yticks(0:2.50:100)
xticks(-1:1:35); xlim([-1 35])
xlabel('Spessore provino (cm)','FontSize',18)
ylabel('Umidità relativa aria (%)','FontSize',18)
set(gca,'FontSize',15)
grid on
xline(assex(1),'k',{'Superficie interna'});
xline(assex(2),'k',{'Freno a vapore - lato interno'},'LabelHorizontalAlignment','left');
xline(assex(3),'k',{'Freno a vapore - lato esterno'});
xline(assex(4),'k',{'Superficie esterna'});
% create the line once:
my_line = line('XData',assex,'LineStyle','-','Color',[0 0.4470 0.7410]);
for k=1:60:size(rh,1)
% set the line's YData each time:
set(my_line,'YData',rh(k,:))
title(string(strcat('Tempo= ',num2str(k/60,'%.2f'),' ore (',num2str(k/(60*24),'%.2f'),') giorni.')))
drawnow
end
  4 个评论
Maja
Maja 2023-7-13
Thanks for the indication, done.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by