for loop for multiple functions and output
Afficher commentaires plus anciens
Good Morning All,
Probably a silly question for most programmers but I am trying to create a loop that will run through two matlab functions.
I have two functions, boreprofile and fcoeffs_fft that I need to run for an iteration of n.
The two functions have the following input/output:
[profile, angles]=boreprofile(dn,CenterCoor,ImportDisp,ImportCoor);
[a0,ak,bk]=fcoeffs_fft(profile,angles,5);
Normally when I run the script I have to manually enter dn...for example:
[profile1, angles1]=boreprofile(1,CenterCoor,ImportDisp,ImportCoor);
[a01,ak1,bk1]=fcoeffs_fft(profile1,angles1,5);
Ak1=sqrt(ak1^2+bk1^2);
...
[profile2, angles2]=boreprofile(2,CenterCoor,ImportDisp,ImportCoor);
[a02,ak2,bk2]=fcoeffs_fft(profile2,angles2,5);
Ak2=sqrt(ak2^2+bk2^2);
...
I tried to make the for loop as follows but its producing the error "In an assignment A(I) = B, the number of elements in B and I must be the same." So I am assuming its not saving the variables the way I have envisioned.
for i=1:n
%Calculating profiles
[profile(i), angles(i)]=boreprofile(i,CenterCoor,ImportDisp,ImportCoor);
%Using f-coeffs_fft function to obtain fourier coefficients
[a0(i),ak(i),bk(i)]=fcoeffs_fft(profile(i),angles(i),5);
%Amplitude of distortion (Fourier Coefficients)
Ak(i)=sqrt(ak(i).^2 + bk(i).^2)*1000;
end
Thanks!!!
3 commentaires
dpb
le 19 Mai 2014
What are output dimensions? Are they consistent for each run?
The basic answer is you need to encapsulate the various outputs in a cell array; the reference profile(i), etc., is to a single element of an array and so the total output of the function for that variable won't fit in a single element.
Look at
doc arrayfun
for one way
Melissa
le 19 Mai 2014
Melissa
le 19 Mai 2014
Réponse acceptée
Plus de réponses (1)
Aravindhan Anbarasu
le 2 Oct 2017
Modifié(e) : Walter Roberson
le 2 Oct 2017
w=4;
c=3*10^5;
pt=10*log10(w/0.001);
d=0.5:0.1:5;
f=[830*10^6, 1880*10^6, 5.5*10^9];
for f=1:3
pr=pt-21.98+20.*log10(c/f)-20.*log10(d);
plot(log10(d),pr)
hold on;
grid on;
end
My y axis is different if I run this code if i use for loop and if i'm not using for loops the answer varies .
w=4;
c=3*10^5;
pt=10*log10(w/0.001);
d=0.5:0.1:5;
f=830*10^6
pr=pt-21.98+20.*log10(c/f)-20.*log10(d);
plot(log10(d),pr)
hold on;
f1=1880*10^6
pr1=pt-21.98+20.*log10(c/f1)-20.*log10(d);
plot(log10(d),pr1)
hold on
f2=5.5*10^9;
pr2=pt-21.98+20.*log10(c/f2)-20.*log10(d);
plot(log10(d),pr2)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!