Why does csvread behave differently for large csv files?
Show older comments
I have two csv files that I'm trying to read in. The first contains one row of integers, the second contains one row of floats.
They are both formatted in the same way (with a trailing comma):
int_val_1,int_val_2,...,int_val_n,
float_val_1,float_val_2,...,float_val_m,
As I understand it, csvread should produce a row matrix with an extra 0 at the end (due to the trailing comma). In my case, however, csvread produces a column matrix without an extra 0 for the first file, and a row matrix with an extra 0 for the second file. This only happens if the first file is large (e.g., 589824 integers). If there are a small number of integers, it behaves as expected.
What's going on?
1 Comment
Jeremy Hughes
on 8 Jun 2015
Edited: Jeremy Hughes
on 8 Jun 2015
Hi Peter,
You have run into an unfortunate limitation in the way csvread detects the number of columns in the file. Since your file is one long row, csvread assumes it's all one never-ending string of data. (at 100,000 columns, as Per discovered below, it stops counting and just returns a column.)
If you want to get consistent results on the output shape, you can call textscan in the following way.
fid = fopen(filename);
[data] = textscan(fid,'%f','Delimiter',',','EndOfLine','\r\n');
fclose(fid);
The variable "data" will be a cell array containing a column of numbers. If you need a row, just pull it out of the cell array and transpose;
data = (data{1})';
I hope this helps,
Jeremy
Accepted Answer
More Answers (0)
Categories
Find more on Standard File Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!