Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function in Matlab similar to the java function String.split(delimiter)?

Tags:

string

matlab

Is there a (default) Matlab function that behaves similar to the java method split(delimiter), where you can tokenize a string based on an arbritary delimiter?

like image 469
robguinness Avatar asked Aug 29 '12 09:08

robguinness


2 Answers

There is a built-in function called textscan that's capable of this:

>> C = textscan('I like stack overflow', '%s', 'delimiter', 'o');    
>> C = C{1}

C = 
    'I like stack '
    'verfl'
    'w'
like image 173
Rody Oldenhuis Avatar answered Sep 28 '22 12:09

Rody Oldenhuis


Here are more than one ways to split a string. One as Rody Oldenhuis has just mentioned, and here are some others:

1> Using the function regexp :

>> str = 'Good good study Day day up';
>> regexp(str,'\s','split')
ans = 
    'Good'    'good'    'study'    'Day'    'day'    'up'
>> 

2> Using the function strread:

>> str = 'Section 4, Page 7, Line 26';
>> strread(str, '%s', 'delimiter', ',')
ans = 
    'Section 4'
    'Page 7'
    'Line 26'
>> 
like image 45
Eastsun Avatar answered Sep 28 '22 13:09

Eastsun