Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Walter Roberson
Walter Roberson le 15 Juil 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]};

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by