Why does csvread behave differently for large csv files?
古いコメントを表示
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 件のコメント
Jeremy Hughes
2015 年 6 月 8 日
編集済み: Jeremy Hughes
2015 年 6 月 8 日
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
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Standard File Formats についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!