File Exchange

image thumbnail


version (3.82 KB) by Amos Storkey
Loads character seperated data with variable length lines and mixed types.

1 Download

Updated 13 Jan 2010

View License

Load any text-based data into MATLAB.

Loads data into cell array. Data can have variable length lines or be mixtures of numeric and string types.

[a,b,c]=loadcell(fname,[',' char(9)],'"','single-empty2num')

Will load tab or comma seperated values into a. Seperators lying between inverted comma pairs will be ignored. Repeated seperators will not be treated as one. All empty fields will be treated as numeric zeros rather than empty strings. b and c give further info. See help.

Other files:

celltonumeric.m will take a columnar cell array and map it to an entirely numeric array with numeric pointers into string arrays. Useful for actually doing stuff with the data you loaded!

datato1ofm.m will remap categorical data (e.g. that produced by celltonumeric) and represent it entirely as 1ofM binary data. It keeps track of what came from where so can be used form multinomial analysis too. Ideal for any count-based probabilistic analysis.

Comments and Ratings (26)

Wonderful function. Saves me a lot of time. One problem is that when I load data, the last row of the variable has blanks or some other weird character.

Mark Cejas

Thanks a ton for this elegant solution. I am having trouble getting it to work...I keep getting the following error:

>> [lc,dflag,numdata]=loadcell('general categories',[',' char(9)],'"','single-empty2num')
??? Undefined function or variable "dflag".

Error in ==> loadcell at 200

any help for this newbie would be greatly appreciated :)

I have updated this and I think that it now competes in speed with readtext on the files I tried, and it has a few nuances that are useful.

I have also added celltonumeric and datato1ofm to file exchange. Just getting the data into a cell array is helpful, as the biggest problem with matlab is getting the data in. However, getting the data into a cell array does not make it very useful. Hence these two files turn this data into more useful numeric formats. They are short and sweet.

m4lte k

I have not updated this recently, and I would now agree that readtext does the most important things loadcell does, but is significantly faster. Hence I would recommend new arrivals at this page to try readtext first (see the Acknowledgements
This submission has inspired the following: readtext link above). Amos

Thierry Dalon

readtext seems faster

Philip Perea

Just learning MatLab (but an experienced programmer) and this program seems to do exactly what I need. Fantastic bit of code Amos. Thanks for sharing it.

Pierre Bushel

I love it. It saves me so much time.

Nacho T.

Super dooper

Mike Good

I have been trying to load this data for hours, and at last, thanks to this, I have now loaded it.

Kent Conover

I thank you for providing this utility.

Amit Reches

Very useful code. Solves some annoying text-file related problems in MATLAB. Thanks!

Ali Asghari

Very nice. Does exactly what I want. (again, good for small dataset)

a hartmann

A good tool that solves a long standing problem.

m hawarey

very good code. speed can be resolved with better machines :):) many thanks to the author.

D. Wagener

Works well but it's kinda slow if you have a larger data file.

M Srinivasan

Powerful and elegant! Very helpful in parsing lines with variable number of fields. I only wish there were a way to speed it up significantly - as fast as, say, Excel, when it opens up similar files.

J Johnson

Pretty powerful.

Mr Andersson

Had some problems but its really OK.

Y Romay

Very helpfull for me to load the data I needed.

John Adams



James Conway

Thank you for adding this. I have advertised this as a "must have" part of any matlab setup to my collegues.

Mike Douglas

The function matlab is missing! I found it so annoying that there is no easy way of just getting data like this (variable length, or mixed type) into matlab in the first place. But now there is... Thanks.

Colin Campbell

Dear Amos:

Please pardon the interruption, but
thank you for writing and posting your
comprehensive "loadcell.m".

It saved me lots of grief massaging
the data I was working with -
basically .csv that Matlab's csvread
choked on because of mixed data types
and missing data. Thanks again!!!

I will be sure to mention your function
in MATLAB courses I give at Waterloo.


P.S. I used:

[lc, dflag, dattype] = loadcell
('test.csv', ',', '', 'single')




Minor updates to enhance speed.

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

Inspired: readtext