Fixed plot and variable-loop plot

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).

 Accepted Answer

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 Comments

Great!! Thank you very much, you saved my day.
@Maja, if the answer solved your problem, please consider accepting the answer.
Accepting the answer indicates that your problem has been solved (which can be helpful to other people in future) and it awards the volunteer with reputation points for helping you.
You can accept only 1 answer for a question, but you can vote for as many answers as you want. Voting an answer also provides reputation points.
Thanks for the indication, done.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 13 Jul 2023

Commented:

on 13 Jul 2023

Community Treasure Hunt

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

Start Hunting!