textscan in a for loop, memory issue
2 次查看(过去 30 天)
显示 更早的评论
I have a text file with 140000 lines, each line has a string at the beginning and then 10 data points. I want to read this file 500 lines at a time and do some calculations based on the string data. So I have a for loop:
for t=1:280
clearvars dataArray
startRow = 1+500*(t-1);
endRow = (500-1) + startRow;
% Open the text file.
dataArray = textscan(fileID, formatSpec, endRow-startRow+1,...
'Delimiter', delimiter, 'HeaderLines', startRow-1,...
'ReturnOnError', false);
x=dataArray{:,3:5};
y=dataArray{:,6:9};
...
...
...
end
the problem is after iteration t=25, textscan doesn't read any data and it returns empty arrays. if I change the for loop to start from 10:
for t=10:280
...
...
end
the error occurs at t=28. It seems to me that there is a memory issue. Is there any way to check what is causing this problem?
回答(2 个)
Harsha Medikonda
2015-7-8
I understand that you wish to know if the behavior you are observing is related to Memory. Please execute the command 'memory' before and after executing the code. This command shows the amount of memory available.
Please refer to the link below for more details on the usage of 'memory' http://www.mathworks.com/help/matlab/ref/memory.html
Another alternative to access large files is using 'datastore'. Please refer to the following link for more details on its usage http://www.mathworks.com/help/matlab/ref/datastore.html
0 个评论
Chris Eguires
2017-6-21
You need to do the following after you do the read; fseek(fileID,0,'bof');
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!