Getting subscripted assignment dimension mismatch error?

1 view (last 30 days)
I am getting a subscripted assignment dimension error for the bit of code I have attached. I'm not sure what this means or how to fix it. Thank you for your help!
Full error message:
Subscripted assignment dimension mismatch.
Error in EMMA_GL4 (line 62) f(:,j)=B' * U_f(:,j);

Accepted Answer

Jan
Jan on 25 Mar 2017
B = [U_GL4_Baseflow(1,1),U_GL4_Tallus0(1,1),U_GL4_snowmelt(1,1); ...
U_GL4_Baseflow(1,2),U_GL4_Tallus0(1,2),U_GL4_snowmelt(1,2)];
X = ('U_GL4_Strm');
Now I guess that B is a [2 x 3] double matrix and X is a string.
X_size = size(X, 2);
mat_1 = ones(1, X_size);
U_f = [mat_1; X];
U_f consists of the unprintable character with teh ascii code 1's in the first row and the characters of the string 'U_GL4_Strm' in the 2nd row:
U_GL4_Strm
This is strange and I have no idea what the intention for this might be. Now the failing line:
f(:,j) = B' * U_f(:,j);
On the right side the [3 x 2] double matrix B' is multiplied by a [2 x 10] char matrix, which yields a [3 x 1] vector in the first iteration. If f is undefined before, this works. If you get an error, the variable f was defined before and its number of columns differs from 3.
Solution: Do not define f before. A good idea is to use a function to keep the workspace clean. Or pre-allocate f correctly:
i = 16;
f = zeros(3, i);
for j=1:i
f(:,j) = B' * U_f(:,j);
end
Then the next problem will occur: U_f has 10 columns only, not 16.
Anyway, the strange mixture of numerical values and the string might be the main problem.
  1 Comment
L. E.
L. E. on 25 Mar 2017
Thanks! I was able to get it going after this. It is a pretty strange set of code - the intention is to model water chemistry mixing and source water in a stream.

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!