Select rows with textscan

조회 수: 8 (최근 30일)
Pap
Pap 2011년 5월 1일
Hi
I use the below text file:
ETE 04/01/2010 10170000 18.54 430 Big Cap
ABC 04/01/2010 10190000 18.34 200 Big Cap
YYY 04/01/2010 10200000 18.34 100 Big Cap
How can I use textscan to import rows with respect to values of column 1? So to import only the rows of 'ETE' for instance and the file to like :
ETE 04/01/2010 10170000 18.54 430 Big Cap
Panos

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 5월 1일
1st approach: bulk import - select
% Import all
fid = fopen('C:\Users\Oleg\Desktop\test.txt');
data = textscan(fid, '%s%s%f%f%f%s%s','CollectOutput',1);
fid = fclose(fid);
% Select only line that begin with 'ETE'
idx = strcmp(data{1}(:,1),'ETE');
data = cellfun(@(x) x(idx,:),data,'un',0);
2nd approach: line by line conditional import
fid = fopen('C:\Users\Oleg\Desktop\test.txt');
data = [];
while ~feof(fid)
tmp = textscan(fid,'%s%s%f%f%f%s%s',1);
if strcmp(tmp{1},'ETE')
data = [data; tmp];
end
end
fid = fclose(fid);
PROS/CONS:
The 1st approach is faster and achieves a discrete memory management for the output without further manipulation of the data BUT it can go out of memory if the file you're importing is huge.
The 2nd approach is slower since there's no preallocation of the output array. From the memory point of view it is inefficient in the way data is stored but at the same time it is efficient since you select only the rows you need. With this approach you can manipulate data inside the IF statement to optimize memory storage to the extreme (ex: instead of big cap you could store numbers to indicate the feature).
  댓글 수: 1
Pap
Pap 2011년 5월 2일
Many Thanks Oleg

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by