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

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2011-7-15
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 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by