Using strings in Matlab

Strings are your friend – Source : Image


Matlab was designed for dealing with numbers, not strings. As opposed to Python, which is an expert at string, Matlab could look sort of limited in this domain. Still you can do many things to manipulate this element. The goal of this post is to introduce you to the usage of strings in Matlab and to help you loosen the knot…

To start with string in Matlab, you just need to use one single character, the . So you can assign a string to a variable this way :

StringVariable='hello world';

If you want to use the character ‘ in your string. Then you just have to double it, like so :

StringVariable='hello world, it''s easy';

As for all objects in Matlab, at the heart of strings are arrays. As such, each string is basically an array of single characters. So given the previous example, you could do :

>> size(StringVariable)

ans =

1 22

There are therefore 22 characters in the variable StringVariable. And as expected, you can change any  element as you wish for instance with :

>> StringVariable(1,10)=’s’

StringVariable =

hello worsd, it’s easy

If you add elements along other dimensions, Matlab automatically populates lines with new characters :

>> StringVariable(2,10)=’s’

StringVariable =

hello world, it’s easy

Of course, contrary to numbers, it is very rare that one actually needs a 2D array of strings. Indeed, it is more likely that you have a list of words that you want to store somehow and since they all have a different number of characters a 2D array is a very inefficient way to store it.

This is when heterogeneous containers, like the CELL, comes handy. Cells are more or less like arrays, except that each elements can be of a different type and/or size. In the case of a list of strings this is very useful as the size issue is properly taken care of.

For instance, if you want to store how to say hello in multiple languages, you will do :

HelloLanguage{3}='Buenos días';

{} is used to access elements in a cell. So :

>> HelloLanguage{3}

ans =

Buenos días

Alright. Now that you know how to play with string, there are a number of things you are very likely to do with this element.

You can combine two strings together the same way you would do with a numerical array :

>> [‘Hello’,’ ‘,’World’]
ans =
Hello World

This work with variable as well :

>> x=’Hello’;
>> y=’World’;
>> [x,’ ‘,y]

ans =

Hello World

Convert from number to string and vice-versa

This can be done (among other possibilities) with str2num, str2double, sprintf, num2str. For instance :

>> x=12;
>> [num2str(x),’ monkeys’]

ans =

12 monkeys

I recommend diving into sprintf if you use strings heavily as it is much faster than num2str and others.

Finding a particular string

There are many ways to do this but at least, to get you started, I would suggest you use strfind and strcmp.

strfind will allow you to check the occurence of a particular sequence of characters in a string, like so :

>> StringVariable=’hello world, it”s easy’;
>> strfind(StringVariable,’world’)

ans =


This entry was posted in Beginners. Bookmark the permalink.

8 Responses to Using strings in Matlab

  1. Very nice explaination. Thank you very much, we are sustaining on internet because of selfless and broadminded people like you.

  2. Manasa says:

    hello, I am working on a TTS (text to speech )method using formant synthesis. i have generated matlab code where in, sound signals are created for each phoneme(letter). Now i need to generate a word using those letters with which sound signals are generated. Please some one help me out

  3. Ronald says:

    Wow, I have to say this awesome, I have always wanted to have dynamic inputs to the the legend function, this just nailed it. You have made my day.

  4. Pingback: MatLab | New ThinKing

  5. Darrin1979 says:

    Hi admin, do you monetize your blog ? There is easy
    way to earn decent money every day, just search on youtube – How
    to earn with wordai 4

  6. DorisX says:

    Hello blogger i see you don’t monetize your site. You can earn additional money
    easily, search on youtube for: how to earn selling articles

  7. Toni Stump says:

    I must thank you for the efforts you have put in writing this
    site. I’m hoping to see the same high-grade content from you later on as well.
    In fact, your creative writing abilities has motivated me to get my
    own, personal website now 😉

  8. Thomas Henry says:

    Nice explanation like your effort which you are putting

Leave a Reply to [email protected] Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.