Filter löschen
Filter löschen

for loop for multiple functions and output

9 Ansichten (letzte 30 Tage)
Melissa
Melissa am 19 Mai 2014
Bearbeitet: Walter Roberson am 2 Okt. 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!!!
  3 Kommentare
Melissa
Melissa am 19 Mai 2014
Oh that makes sense to why that error is happening. I will look at the document and read through. To answer your question for the consistency of the output they will be for a given run but not necessarily every time. For example for one complete loop it may be 72x1 for the profile and angles where if I import anotehr round of data it may be 84x1 for profile and angles another time.
Melissa
Melissa am 19 Mai 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 19 Mai 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.
  4 Kommentare
Melissa
Melissa am 19 Mai 2014
It worked!! Thanks you very much for all of your helpful advice :D
Star Strider
Star Strider am 19 Mai 2014
My pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Aravindhan Anbarasu
Aravindhan Anbarasu am 2 Okt. 2017
Bearbeitet: Walter Roberson am 2 Okt. 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)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by