'While' loop that pulls data from a matrix with n rows

I am trying to write a code to execute a 'while' loop from a matrix. The matrix it pulls the data from ideally has n lines (where n can vary depending on the .xls file uploaded and the number of rows it contains). Variables DRb and PRb are defined earlier in the code and are constants.
[min,el]=xlsread('file name.xls');
while f<=0.04
col=4;
atom=1;
ReverseMelt(row, atom)=f;atom=atom+1;
ReverseMelt(row, atom)=((min(6,col)/(DRb+f*(1-PRb))))^(-1); col=col+1;atom=atom+1;
*many more lines of calculations*
row=row+1;
f=f+0.005;
end
The data I am looking for this calculation starts at min(6,col) but I want the while loop, when it reaches f=0.04, to start again such that min(6,col) becomes min(7,col) and so on till the end of the matrix. I have tried a 'for' loop where I put before the while loop:
x=size(min)
y=x(1,1)
For z=6:y
while f<=0.04
col=4;
atom=1;
ReverseMelt(row, atom)=((min(y,col)/(DRb+f*(1-PRb))))^(-1);
end
end
I thought this would make my while loop run from row 6 in matrix min to y (the number of total rows), but didn't go. Any ideas?

 채택된 답변

Roy Kadesh
Roy Kadesh 2018년 2월 6일

1 개 추천

Your loop variable is z, while you use y inside your loop, so it repeats the same code y times (assuming you have hidden the code that re-initialized f for the second for-loop)

댓글 수: 3

Ah, I see this simple mistake. However, when I insert z in the 'while' loop, I still only get the return of the first row. The code now looks like:
for z=6:y
while f<=0.04
col=4;
atom=1;
ReverseMelt(row, atom)=((min(z,col)/(DRb+f*(1-PRb))))^(-1); col=col+1;atom=atom+1;
But the matrix ReverseMelt will only have saved the loop data of the the first line in matrix min at row 6.
You need to reset f, otherwise the while loop will exit immediately after the z=6 is done. You can see this if you use the debugger to step through your code.
Excellent, this solved it. Thanks!

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

추가 답변 (0개)

카테고리

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

질문:

2018년 2월 6일

댓글:

2018년 2월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by