Feed on

to split a delimited string in matlab, you can use the ‘regexp’ function.  for instance, if i’ve got a string named tline that’s been tab-delimited, i can parse it using:

 >> parts = regexp(tline,’\t’,'split’);

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

12 Responses to “split a delimited string in matlab”

  1. on 23 Feb 2009 at 1:03 pm srividhya

    i have a string..
    i want to split this into uni,ver,sity..
    how can i do this in matlab..
    please reply for me..am badly in need of this solution.
    thank u so much…..

  2. on 26 Feb 2009 at 5:55 pm Peter

    Hey Lawrence,

    1) I really like your photo-navigation! That’s quite creative.

    2) When you are giving help on a particular application such as Matlab, please list the version that you are using. In v6.5.2, I get “Invalid option for regexp: split.” I found that my version of regexp only accepts two arguments anyway.


  3. on 26 May 2009 at 9:28 am Geert

    Hi there,

    Thanks for this! I was driving me crazy that there was no simple solution for this in Matlab. There are some functions around but none as efficient as yours. And, I could not find this in the Matlab reference (R2006b). The ‘split’ option is nowhere to be found.

    GNU Octave has the ‘split’ function that does just this and this was giving me headaches when porting my code to Matlab.

    Thanks again.

  4. on 20 Aug 2009 at 2:42 pm John

    In matlab you have to use the regexp function with the 3rd, optional argument set to ‘split’. It’s silly, but that’s how they chose to do it. Full syntax:

    TargetVar = regexp(‘(any string)’,'(delimiter)’,'split’);

    The output is a cell array (maybe two deep, I don’t remember) with the entries you want. Type help regexp.

  5. on 01 Apr 2010 at 2:22 pm yashu

    hii.. want to divide kanada word into single characters using matlab code .how should i write the code ??’

    plz send me the code ……..
    tanx alot ………:)

  6. on 27 May 2010 at 8:41 am Ramon Fincken

    Excellent, thanks. The answer is indeed a cell array:

    I have filenames like

    To get the ‘A’ I use this in my loop:
    temp = dirList(x).name;
    temp2 = regexp (temp,’__’,'split’);
    imagegroup = temp2{1,1};

  7. on 11 Jun 2011 at 6:41 pm Eric

    For those with old Matlab versions.

    You can split text with strread(), e.g. to split a long string at line breaks, do this:

    lines = strread(text, ‘%s’, ‘delimiter’, ‘\n’);

    This will give you a cell array.
    regexp also works on cell as input, yielding a cell-of-cells as output. strread() doesn’t and you’ll have to loop manually.



  8. on 08 Dec 2011 at 9:48 am Sana

    I tried what you said and the out come was:

    s=’hi this is me’

    s =

    hi this is me

    >> l= strread(s, ‘%s’, ‘,’,'\n’);
    ??? Unknown option ‘,’.

    Error in ==> strread at 51

  9. on 02 Nov 2012 at 1:58 pm Olga

    Thank you so much!

  10. on 09 Apr 2013 at 6:22 am coloring books

    I am fairly certain I have see this same type of declaration somewhere else, it must be gaining interest using the world.

  11. on 15 May 2013 at 8:27 am xyz0k

    It worked for me, thanks! ;)

  12. on 03 Apr 2015 at 12:27 am Meenakshi

    I have binary value in s=1010101111101110001 if i use the function biarytofloat(‘s’) matlab reads a character s not the value which is stored in it

Did I get this wrong? Let me know!

Trackback URI | Comments RSS