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

1 view (last 30 days)
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

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]};

More Answers (0)

Categories

Find more on Characters and Strings 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!