Storing strings of different sizes in a MATLAB array?

16,749

Solution 1

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

You can, however, use a cell array:

userinput={'AJ48 NOT'; 'AH43 MANA'; 'AS33 NEWEF'};

disp(userinput{1});

Be aware that there are many situations where cell arrays don't work like normal arrays.

Solution 2

To just answer to your last part of your question; simply because strings may be variable length but numbers (in Matlab) are fixed length. It's one of the main ideas of arrays to let them hold only fixed sizes entities (for example because the need of efficient look up), see more on the topic here.

Share:
16,749
straits
Author by

straits

Mechanical Engineering graduate. Familiar with Matlab and C++.

Updated on June 15, 2022

Comments

  • straits
    straits almost 2 years

    I want to be able to store a series of strings of different sizes such as

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

    This of course returns an error as the number of columns differs per row. I'm aware that all that is needed for this to work is adequate spaces in the first and second rows. However I need to be able to put this into an array without forcing the user to add these spaces on his/her own. Is there a command that allows me to do this? If possible I'd also like to know why this problem doesn't arise with numbers e.g.

    a=[1; 243; 23524];

  • Mikhail Poda
    Mikhail Poda almost 13 years
    In my opinion it is not an optimal approach to teach MATLAB cell arrays as something "not normal". Is it not better to say the it right from the start - all arrays in MATLAB are "normal", whether double, struct or cell. And all arrays work the same! Beyond that cell arrays have some additional shortcut syntax.
  • Oliver Charlesworth
    Oliver Charlesworth almost 13 years
    @Mikhail: Cell arrays are very much not the same as "normal" arrays. They are slower, less flexible in some circumstances, and more flexible in other circumstances.
  • Mikhail Poda
    Mikhail Poda almost 13 years
    Sure, optimal use cases for double, struct or cell arrays are different, but array operations are identical - access, reshape, delete, add elements.
  • Oliver Charlesworth
    Oliver Charlesworth almost 13 years
    @Mikhail: That's a fairly small subset of the operations that Matlab offers! For instance, you can't really do vectorised operations on cell arrays. You can't do maths on cell arrays. You can't search a cell array. You can't sort an array (except in a few special cases). etc.
  • Mikhail Poda
    Mikhail Poda almost 13 years
    I agree, vectorized operations is the shortcut syntax only for double arrays. Math makes little sense on cell arrays because cell arrays are used in 99% of cases for strings or function handles. Search is same as vectorized. Sorting works where it makes sense, i.e. where objects can be compared like strings. For many years I thought of cell and double arrays as something different. Personally, I view it as achievement to understand that fundamentally all arrays share same operations + have additional syntax shortcuts.
  • Oliver Charlesworth
    Oliver Charlesworth almost 13 years
    @Mikhail: It is more than just "syntax shortcuts"; the whole ethos of Matlab is to use vectorised operations wherever possible, because they're far more efficient!
  • Mikhail Poda
    Mikhail Poda almost 13 years
    @Oli: I just posted a MATLAB question which resulted from our discussion!
  • Mikhail Poda
    Mikhail Poda almost 13 years
    @Oli: why would you use a cell arrays to store a double matrix? Your message is that cell array are "other", "bad", slow arrays. But I do not see the overlap bw. double and cell array use, each is very well adapted for its own use case. And their share some common basic syntax
  • Oliver Charlesworth
    Oliver Charlesworth almost 13 years
    @Mikhail: That is precisely my point. Cell arrays serve a very different purpose, and so work completely differently.
  • Mikhail Poda
    Mikhail Poda almost 13 years
    @Oli: "completely" is a laaaaarge exaggeration IMO!
  • Mikhail Poda
    Mikhail Poda almost 13 years
    this discussion is being continued here: stackoverflow.com/questions/6499296/…