MATLAB Answers

0

Legend in a "for loop"

Asked by Paul Rogers on 10 Nov 2019 at 19:33
Latest activity Commented on by TADA
on 11 Nov 2019 at 11:14
hi,
I need to insert a legend for each ii evalueted.
giri can change in size at every simulation.
for ii=1:length(giri)
figure(3);
plot(m(indice_m_0:loc(ii)),psic_pos(ii,indice_m_0:loc(ii)));
grid on
hold on
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Shubham Gupta on 11 Nov 2019 at 3:13

Try :
for ii=1:length(giri)
figure(3);
plot(m(indice_m_0:loc(ii)),psic_pos(ii,indice_m_0:loc(ii)),'DisplayName',['ii = ',num2str(ii)]);
grid on
hold on
end
legend show

  5 Comments

Paul Rogers on 11 Nov 2019 at 11:00
yes, but I don't know why, instead of the ii-value it shows chines letters.
I attached the file.
Walter Roberson
on 11 Nov 2019 at 11:08
figure(3);
for ii=1:length(giri)
plot(m(indice_m_0:loc(ii)),psic_pos(ii,indice_m_0:loc(ii)),'DisplayName',sprintf('giri = %g',giri(ii)));
grid on
hold on
end
legend show
Change the %g format to whatever you need.
TADA
on 11 Nov 2019 at 11:14
probably because of an implicit conversion from double to char
maybe this would work:
plot(m(indice_m_0:loc(ii)),psic_pos(ii,indice_m_0:loc(ii)),'DisplayName',['giri = ', num2str(giri(ii))]);
next time it would be better if you attach the data, without sample data helping you is pretty much guesswork...

Sign in to comment.