File Exchange

image thumbnail

Split delimiter separated strings into a matrix

version 1.0.0.0 (2.05 KB) by Gie Spaepen
Function to split a delimiter separated string into a matrix of doubles.

2 Downloads

Updated 16 Mar 2004

View License

Function: outmatrix = strsplit(inpstr[,delimiter])

Input:
inpstring, this is the input string which is a delimiter separated string: 345.56,376.5,4334.34 .
delimiter, optional if not given, ',' is taken.
outmatrix: if all values in inputstring are numerical a double array is given otherwise a cell array.

Cite As

Gie Spaepen (2020). Split delimiter separated strings into a matrix (https://www.mathworks.com/matlabcentral/fileexchange/4615-split-delimiter-separated-strings-into-a-matrix), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

Gie Spaepen

Thanks for all the comments and downloads. I left my lab for a while now but I will implement your suggestions and make a new release of this script.

Matthias Pospiech

This code has a bug for delimiters longer than 1 char. You need to change this lines to :

strtpos = idx(i-1) + sizedelimiter;
tempsplit(i+1) = {inpstr(idx(i)+ sizedelimiter : endpos)};

@Schwarz: if strread does the same, and probably textscan too, why does matlab not provide the necessesary documentation and examples ?

Flurin Honegger

The handling of the "no delimiter in string .." case is not necessary and results in an inconsistent interface that results in unnecessary checking of return values.
I would leave out the check on "size(idx) == 0" and initialize
tempsplit as follows tempsplit = {inpstr};

Thank you for posting this routine that in contrast to regexp(s,'\t','split') (since R2007b) works also on older Matlab versions.

jjcai Cai

jjcai Cai

One of the problems of this function is that it does not work with delimiter '\t'.

One of many solutions by
u...@neurol.unizh.ch (I found from google groups) is as follows:

>> s=sprintf('one\ttwo three\tfour');
>> r=regexp(s,'\t','split')

Siamak Faridani

Nikola Toljic

Great. Thanks.

Joel Sander

It was simple and did exactly as advertised.

Urs Schwarz (us)

strread will do the same - and - it comes with more options

Michael Robbins

I could be wrong, but I think there is a much better, vectorized, solution available on CSSM

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: rsplit, latexfigure

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!