Using strings in Matlab

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
s

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{1}='Hello';
HelloLanguage{2}='Bonjour';
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.

Concatenate
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 =

7

Related Posts

This entry was posted in Beginners. Bookmark the permalink.

2 Responses to Using strings in Matlab

  1. theakshay.jain@gmail.com says:

    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

Leave a Reply