Quantitative finance collector
C++ Matlab VBA/Excel Java Mathematica R/Splus Net Code Site Other
Sep 9

Reading CSV in Matlab

Posted by abiao at 15:43 | Code » Matlab | Comments(4) | Reads(26744)
I always believe R is better than Matlab in terms of data loading, for example, read.table() is able to read a large tab-deliminated file (such as CSV file) easily. You may argue there is also csvread() function in Matlab, but don't forget, csvread() works only with all numeric, or all text data, but not for multiple format file, which is often used in practice, for instance, a file containing both bond coupon rate and bond issuer name, etc.

Let's say you have a csv file with structure like below
csv file to import in Matlab
How to import it in Matlab? csvread() obviously doesn't work as this file has mixed format, and it returns error
??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 1, field 1) ==> CUSIP

Error in ==> csvread at 52
    m=dlmread(filename, ',', r, c);

dlmread() does not, either. textscan() may work at the cost of inflexibility.

Luckily I found a function called readtext(), it can read whatever text (file) you give it, and return an array of the contents, no matter what the format your file is at. Using the same csv file above yields  {13*5} cell for further data handling.

Perfect, download & test yourself at http://www.mathworks.com/matlabcentral/fileexchange/10946-readtext

Tags: ,
Wouldn't you need to write to a cell array?
I needed a way to import data from CSV with multiple format.
csvimport worked for me.
I'm definitely going to bookmark your blog, I just love your post, thanks for such a nice sharing..Hope to get some info on your blog in future. armor games
Pages: 1/1 First page 1 Final page
Add a comment
Enable HTML
Enable UBB
Enable Emots
Nickname   Password   Optional
Site URI   Email   [Register]