How to create plot legend in a loop
7 ビュー (過去 30 日間)
古いコメントを表示
I am attempting to add a legend for frequency response plots that are generated in a loop.
I have read through a number of similar answers on this topic but I have not found any that seem to work.
One of the solutions I found mentions indexing a string variable by curly brace but this
just causes an error "Unable to perform assignment because brace indexing is not supported for
variables of this type". Then I went through a blog post that discussed brace indexing but I did not
find a relevant solution there either. I also tried using smooth braces on the 'txt' variable but then
matlab complains that the size of the variable on the left does not match that on the right, even
though they both use the same index.
I would greatly appreciate it if someone could offer a solution to this issue.
Below is my code.
Thank you.
w = 0.01:0.01:20;
zeta = [0.1 0.3 0.707 1.0 3.0 10.0];
w0 = 1;
H = zeros(1, length(w));
figure()
plot(0,0)
hold on
index = 1:1:length(w);
for k=1:length(zeta)
txt{k} = strcat('\zeta', num2str(zeta(k)));
H(index) = abs(1./(1+(2*zeta(k)*1j*w./w0)+(1j*w./w0).^2));
plot(0:0.01:20-0.01, H);
end
hold off
legend(txt)
xlabel('Frequency \omega/\omega_o')
ylabel('Amplitude')
title('Frequency Response |H(j\omega)|')
3 件のコメント
Mohammad Sami
2021 年 7 月 8 日
I think most likely what happened was that txt variable was already created elsewhere and it was not a cell array. If that was the case matlab will give you an error when you try to do txt{k}.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!