reading excel data in for loop

1 view (last 30 days)
Hi. I intend to assign the value of each cell of TT(x,y) inside the for loop.
A: is a 501*584 matrics including zero and non-zero values.
solid is a 501*584 matrics including 0 and 1 value (0 and 1 indicate the liquid and solid area, respectively).
Lx is the number of rows (501), and Ly is the number of columns (584).
but when the code is run, the "subscripted assignement dimension mismatch" error message related to the line number 5 was appeared. Highly appreciate any solution for this problem.
1- T = xlsread ('A');
2- for y=1:Ly
3- for x=1:Lx
4- if solid (x,y)==0
5- TT(x,y)=T;
6- else
7- TT(x,y)=0;
8- end
9- end

Accepted Answer

Walter Roberson
Walter Roberson on 4 Nov 2019
TT(x,y) = T(x,y);
You are currently trying to assign all of T into the specific location TT(x,y)
Note: if I am corect that you want T(x,y) then your code can be made much more compact, with no for loops at all:
TT = T .* (solid == 0);
When solid is non-zero then solid==0 is false, which is 0, and 0 times anything finite is 0 is the result of the expression.
When solid is 0 then solid==0 is true, which is 1, and 1 times anything numeric is the value itself.
No loops needed.
Note: if some T values might be infinite where solid is not zero then 0*infinity is nan instead of being 0, so if that is a realistic case, you should consider
TT((solid == 0) & isnan(TT)) = 0;
  2 Comments
Adel Gohari
Adel Gohari on 5 Nov 2019
many thanks for your smart answer. I've never looked to the issue from the point of view you've mentioned.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!