Azzera filtri
Azzera filtri

Matlab for loop plots curves in random order

1 visualizzazione (ultimi 30 giorni)
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)

Risposta accettata

Star Strider
Star Strider il 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 Commenti
Arthur Moya
Arthur Moya il 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 il 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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by