Working with a big CSV file in MATLAB

爱⌒轻易说出口 提交于 2019-12-03 16:06:05

You should probably use textscan to read the data in in chunks and then process. This will probably be more efficient than reading a single line at a time. For example, if you have 3 columns of data, you could do:

filename = 'fname.csv';
[fh, errMsg] = fopen( filename, 'rt' );
if fh == -1, error( 'couldn''t open file: %s: %s', filename, errMsg ); end
N  = 100; % read 100 rows at a time
while ~feof( fh )
  c  = textscan( fh, '%f %f %f', N, 'Delimiter', ',' );
  doStuff(c);
end

EDIT

These days (R2014b and later), it's easier and probably more efficient to use a datastore.

There is good advice on handling large datasets in MATLAB in this file exchange item.

Specific topics include:
* Understanding the maximum size of an array and the workspace in MATLAB
* Using undocumented features to show you the available memory in MATLAB
* Setting the 3GB switch under Windows XP to get 1GB more memory for MATLAB
* Using textscan to read large text files and memory mapping feature to read large binary files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!