textscan in a for loop, memory issue

조회 수: 2 (최근 30일)
Orion
Orion 2015년 7월 6일
답변: Chris Eguires 2017년 6월 21일
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?
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 6일
Why memory issue after 10 iterations?

댓글을 달려면 로그인하십시오.

답변 (2개)

Harsha Medikonda
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

Chris Eguires
Chris Eguires 2017년 6월 21일
You need to do the following after you do the read; fseek(fileID,0,'bof');

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by