split a delimited string in matlab
November 6th, 2008 by Lawrence David
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’);
hi..
i have a string..
a=’university’
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…..
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.
Thanks.
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.
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.
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 ………:)
Excellent, thanks. The answer is indeed a cell array:
I have filenames like
A__somethinghere.png
To get the ‘A’ I use this in my loop:
temp = dirList(x).name;
temp2 = regexp (temp,’__’,'split’);
imagegroup = temp2{1,1};
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.
HTH,
Eric
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
[varargout{1:nlhs}]=dataread(‘string’,varargin{:});
Thank you so much!
I am fairly certain I have see this same type of declaration somewhere else, it must be gaining interest using the world.
It worked for me, thanks!
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