Matlab for loop plots curves in random order

1 次查看(过去 30 天)
Hi everyone,
What I am doing with the code below is to plot a curve of res_p vs P_e for every value of def_err. However, this produces over 50 plots and I would like to plot specific indices within def_err, namely 10, 20, 30, 40 and 50.
I suspect that I may be indexing into the variable def_err incorrectly such that the output plots are in random order(see plots below). And the values of the phase error seem oddly small as well.
clear all;
% P_e=pi/8;
lambda= 1.94e-2; %Ang
res_p= linspace(0,1.5,50); %Ang^-1
def_err = (0.4:0.4:20);%Ang
idx=[10, 20, 30, 40, 50];
A=def_err(idx);
for j=1:length(A);
P_e(:,j)= pi*lambda.*def_err(:,j).*res_p.^2;
for i=1:length(res_p)
hold on
plot(res_p,P_e.*(180/pi));
end
legend ('4 A','8 A','12 A','16 A','20 A','Location','best','Fontsize',14);
end
ylabel('Phase error (degrees)','Fontsize',14)
xlabel('Spatial frequency (q) (Ang^{-1})', 'Fontsize',14)

采纳的回答

Star Strider
Star Strider 2021-4-23
The code required a bit of tweaking.
Try this —
lambda= 1.94e-2; %Ang
res_p= linspace(0,1.5,50); %Ang^-1
def_err = (0.4:0.4:20);%Ang
idx=[10, 20, 30, 40, 50];
A=def_err(idx);
for j=1:length(A);
P_e(:,j)= pi*lambda.*def_err(:,j).*res_p.^2;
end
figure
hold on
for i=1:length(A)
plot(res_p,P_e(:,i).*(180/pi), 'DisplayName',sprintf('%2d A',A(i)));
end
legend ('Location','best','Fontsize',14);
ylabel('Phase error (degrees)','Fontsize',14)
xlabel('Spatial frequency (q) (Ang^{-1})', 'Fontsize',14)
hold off
.
  4 个评论
Arthur Moya
Arthur Moya 2021-4-24
@Star Strider,Thank you again.
I guess I have to keep doing this until I build enough experience-backed confidence.
Cheers,
Arthur Moya
Star Strider
Star Strider 2021-4-24
No worries!
I first encountered MATLAB in 1993, and I have been involved in Answers on and off since 2012. It just takes practice!

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by