# for loop for multiple functions and output

24 views (last 30 days)
Melissa on 19 May 2014
Edited: Walter Roberson on 2 Oct 2017
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!!!
Melissa on 19 May 2014
Changed it and removed the indexing in the profile, angles, ak and bk. The code works but now overwrites every time. So another issue which I will google to attempt to find the solution that way.
for i=1:n
%Calculating profiles
[profile, angles]=boreprofile(i,CenterCoor,ImportDisp,ImportCoor);
%Using f-coeffs_fft function to obtain fourier coefficients
[a0,ak,bk]=fcoeffs_fft(profile,angles,5);
%Amplitude of distortion (Fourier Coefficients)
Ak=sqrt(ak.^2 + bk.^2)*1000;
end

Star Strider on 19 May 2014
The outputs profile(i) angles(i) appear to be vectors, since you’re taking the fft (or inverse fft) of them.
If they’re in a row vector, name them as angles(i,:), if a column vector angles(:,i). The same for the results of fcoeffs_fft, if they’re vectors as well.
BTW, it’s best not to use ‘i’ and ‘j’ as index references, since MATLAB uses them as its imaginary operators. Using them as index references could create confusion.
Star Strider on 19 May 2014
My pleasure!

Aravindhan Anbarasu on 2 Oct 2017
Edited: Walter Roberson on 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)