Matlab for loop plots curves in random order

2 views (last 30 days)
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)

Accepted Answer

Star Strider
Star Strider on 23 Apr 2021
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 Comments
Arthur Moya
Arthur Moya on 24 Apr 2021
@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 on 24 Apr 2021
No worries!
I first encountered MATLAB in 1993, and I have been involved in Answers on and off since 2012. It just takes practice!

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!