Select rows with textscan
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!