Effective way to extract rows (range) from a text file

조회 수: 8 (최근 30일)
Sultan
Sultan 2019년 5월 12일
댓글: Jeremy Hughes 2019년 5월 15일
I made a small loop to extract rows [range 183:262326] to make a column of 262144x1 column vector. The loop is very inefficient and takes a lot of time to execute. The code is below. Is there any more efficient way ? I dont want to use "Import Data" feature in MATLAB, I want to use code only.
fid=fopen('ABC.txt');
j=1;
for i=183:262326
linenum = i;
C(j,1) = textscan(fid,'%f',1,'delimiter','\n', 'headerlines',i-1);
j = j+1;
end
  댓글 수: 2
Image Analyst
Image Analyst 2019년 5월 12일
Maybe, but you forgot to attach abc.txt so we can't really help very effectively until you do. Maybe dlmread() or importdata() would do the trick, but we don't know until you attach the text file.
Sultan
Sultan 2019년 5월 15일
편집: Sultan 2019년 5월 15일
Unfortunately the textfile "ABC.txt" is a very large file 25MB even compressing the file proved uselss. But I used the command dlmread() first then used readmatrix() as the former is now not recommended. readmatrix() extracted all the numeric data and output was numeric matrix.

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

채택된 답변

Sultan
Sultan 2019년 5월 15일
Unfortunately, due to very large size of "ABC.txt" file, it could not be uploaded. However following the hints of both the commentors. I simply used readmatrix() to extract all the numeric data. The extraction is much efficient.
A = readmatrix('ABC.txt')

추가 답변 (1개)

Jeremy Hughes
Jeremy Hughes 2019년 5월 12일
Readling line by line is quite ineffecient.
Try this:
C = textscan(fid,'%f','delimiter','\n', 'headerlines',182);
  댓글 수: 2
Sultan
Sultan 2019년 5월 15일
It worked thanks alot, but it is limited to reading only a single row. My intention was to extract the entire column. The problem was solved using readmatrix().
Jeremy Hughes
Jeremy Hughes 2019년 5월 15일
There must be something else in the file other than numbers that causes textscan to stop after the first number.
Glad you found a solution.

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

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by