Efficient CSV Reader for MATLAB

This CSV reader will provide you with uncompromised performance. Thanks to the power of MEX compiler, for the first time in history you can load _HUGE_ CSV files into MATLAB in seconds. Enjoy!


quote = '"';
sep = ';';
escape = '';
[numbers, text] = swallow_csv(filename, quote, sep, escape);

The attached archive contains compiled MEXW64 version. For other OS/architecture you need to:

mex swallow_csv.cpp

DOWNLOAD: swallow_csv.zip


  • This is truly awesome, the original csvimport function i used took 2 hours to load a single csv and this is like 10 seconds

    • Hi Cecil,

      May I ask how to use the csv reader? I am an absolute beginner for matlab and I would like to import a 1.3G .csv file data in matlab. All the data in the file is one header line and the other are five columns of eight decimal numbers.


  • This is light years faster than anything that I have used. Do you have a version that reads a certain range in the csv file ? In the original csvread in MATLAB you can specify a range from which to read from (row and column to row and column). The tool is already hugely useful but this addition will make it immensely hugely useful 🙂

    Thanks for this, it’s great !

  • Thanks a lot. Tried in vain to get textscan to read a file with datestrings,'”‘-escaped data and some missing values, this solved all problems + it is quick. Good work!

  • There is an error in the cpp file; the escape character is not in the if-else chain of characters that may be escaped, preventing literal escape characters from being included in fields. I recommend inserting:
    } else if (buf[i] == escape) {
    cell += escape;
    below line 111.

    Otherwise, this library worked charmingly.

  • Hi,
    I was wondering if you have a 32bit compiled MEX.I have a problem with my version of Matlab I use and a free C++ compiler to compile mex.Any help would be greatly appreciated.
    Many Thanks

  • I got an Out of memory error on importing a 1.9Gb csv file, so I’m not too impressed with this program. I’ll keep searching for an alternative method.

    • For me the program didn’t work because of “Encountered unknown escape sequence”. Tried to fix it but no solution yet..

  • Here, I have seen many discussions with respect to .CSV to .MAT file converter, but still I am little bit confused on this. Can any one help me to write a Matlab code which converts .csv file to .mat file dynamically.

    Here is the first 4 lines of csv file. Sorry I am not allowed to post an csv file here, or I would have.

    [Name] M1 Speed M1 Shaft Torque M2 Speed M2 Shaft Torque [Unit] min-1 N.m min-1 N.m 1 0 0.16805 0 -0.01404 2 0 0.16301 0 -0.01042

    FYI: this is just a sample, My CSV file has 40 columns and 48076 rows. First 2 rows and 2 columns has alphabets, then down remaining all rows n columns has only numbers.

    thanks – ksc

  • I have tried this function. It is awesome. However, it can only load the first 1048575 rows of the files… Is it possible to load all data from the file.
    By the way, what is the meaning of ‘quote’, ‘sep’, and ‘escape’. Since there is no introduction of these variable, it is hard to apply it in different cases.

    Thanks a lot!


  • Hello,
    I have an excel sheet which has 400000 rows and 22 columns. I have used xlsread to read file, but its taking lots of time. Please help to read it faster.

  • Wow, how can this be faster than matlab’s CSV reader? You are just reading the file in chunks. Can matlab’s reader be that bad!?!

  • IT DOESNT WORK WITH COMMA SEPERATED VALUES, what is the meaning is not explained, whoever says it is useful please confront it with me

Leave a Reply

Your email address will not be published. Required fields are marked *