Saving strings of different sizes in a MATLAB array?

I want to be able to store a number of lines of different sizes, for example

userinput=['AJ48 NOT'; 'AH43 MANA'; 'AS33 NEWEF'];

This, of course, returns an error, since the number of columns is different in each row. I know that all that is needed for this is adequate spaces in the first and second lines. However, I need to be able to put this in an array, without forcing the user to add these spaces on their own. Is there a team that allows me to do this? If possible, I would also like to know why this problem does not occur with numbers, for example.

a=[1; 243; 23524];

+4
string arrays matlab store
source share
2 answers

You cannot do this with standard Matlab arrays. A string is just a character vector in Matlab. And you cannot have a matrix with rows of different lengths.

However, you can use an array :

 userinput={'AJ48 NOT'; 'AH43 MANA'; 'AS33 NEWEF'}; disp(userinput{1}); 

Remember that there are many situations where cell arrays do not work like regular arrays.

+8
source share

To simply answer your last part of your question; simply because strings can be of variable length, but numbers (in Matlab) are fixed length. This is one of the main ideas of arrays that allows them to hold objects of a fixed size (for example, due to the need for efficient search), for more details see the section here .

+1
source share

All Articles