MATLAB Answers

Error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts" when loading a string into a matrix

2 views (last 30 days)
Sam
Sam on 14 Jul 2011
Hi,
I am trying to load to columns of string into a matrix using the following code:
ABC(xy,1) = num2str(pos); ABC(xy,2) = [' ' stri strj];
xy is the index, "pos" is a four digit number and stri & strj are two strings. When the first line runs I get the error: "Assignment has more non-singleton rhs dimensions than non-singleton subscripts" The variable "pos" is converted successfully but when I try to store it in the array it gives me the error. Using just a single digit works fine.
Any ideas?
Thank you,
Sam

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Jul 2011
You cannot have columns of separate strings in any kind of matrix other than a cell array.
Strings are not single objects in MATLAB: they are arrays of characters, so whenever pos has more than one digit, you are trying to store the array with multiple positions in to a single location ABC(xy,1); as you have discovered, that fails.
Consider instead using
ABC{xy,1} = num2str(pos);
ABC{xy,2} = [' ' stri strj];
Or alternately,
ABC(xy,:) = {num2str(pos), [' ' stri strj]};

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.