for loop for multiple functions and output

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

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
Melissa 2014년 5월 19일
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.
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
Star Strider 2014년 5월 19일

0 개 추천

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

Melissa
Melissa 2014년 5월 19일
편집: Melissa 2014년 5월 19일
Thanks Star Strider. I will change the i and j to another variable name and after dpb said something similar above I realized that was the error. I changed my indexing and now I cant figure out how to store Ak since only the last iteration is being stored. I even used what I believe is pre-allocation and set Ak=zeros(n,5) before the loop. Any suggestions?
Ak=zeros(n,5);
for i=1:n
[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
Star Strider 2014년 5월 19일
편집: Star Strider 2014년 5월 19일
You need to index Ak and any others you want to store to keep all the interim values. How you do that depends on the size of Ak.
If it’s a scalar, just refer to it as Ak(index).
If it’s an array and doesn’t change size between loop iterations, use the indexing scheme I suggested for angles in my Answer.
If your variables are different sizes between loops, you may have to use a cell array. Cell arrays complicate things slightly, but are extremely useful.
Melissa
Melissa 2014년 5월 19일
It worked!! Thanks you very much for all of your helpful advice :D
Star Strider
Star Strider 2014년 5월 19일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Aravindhan Anbarasu
Aravindhan Anbarasu 2017년 10월 2일
편집: Walter Roberson 2017년 10월 2일

0 개 추천

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)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2014년 5월 19일

편집:

2017년 10월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by