Read a .txt except one column

Hello,
I come to you because I have a problem to import a .txt. In fact, I have some .txt files following the same format, namely : 30 lines of headers, and then a format like that :
D; Q; valQ; Ptot; Fsol; ETP; Temp; Vent; Humi; DLI; SSI; IHGR; SWI;
19580801; -99; -; 0.0; 0.00; 4.0; 19.9; 2.3; 10.; 2827.9; 2495.2; -0.999; 0.435;
I tried different function (dlmread, textscan, ...) but I have a problem with the third column because it's not an integer. I'm not interested by these column. So, I would like to have a matrix in Matlab, from the .txt, without the third column. Do you know how to help me ? Thanks !

1 件のコメント

per isakson
per isakson 2016 年 3 月 19 日
編集済み: per isakson 2016 年 3 月 19 日
Try something like
cac = textscan( fid, '%f%f%*s%f%f%f%f%ff%f%f%f', 'Headerlines',30+1 ...
'Delimiter',';', 'CollectOutput',true )
and study the documentation of textscan

回答 (2 件)

dpb
dpb 2016 年 3 月 19 日
編集済み: dpb 2016 年 3 月 20 日

0 投票

Looks like an extra count for 'headerlines', maybe? 'Pends on whether the above line is the 30th or the previous 30 were some other text and then the variable names...
I'd only point out here's a place where the use of repmat to count repeat fields is handy rather than trying to count how many '%f' fields one has typed in...
fmt=['%f%f*%f' repmat('%f',1,10)];
cac = cell2mat(textscan(fid,fmt, 'Headerlines',30+1 ...
'Delimiter',';', 'CollectOutput',true ));
Also cell2mat wrapped around textscan gets the double array directly instead of a cell array which is more pain to dereference when not needed.

2 件のコメント

Walter Roberson
Walter Roberson 2016 年 3 月 19 日
I think you meant textscan not testscan
dpb
dpb 2016 年 3 月 20 日
Ayup...corrected typo...
Thomas DENIS
Thomas DENIS 2016 年 3 月 20 日

0 投票

Allright, thanks you a lot both of you ! I managed to do it by using :
fid = fopen('abc.txt');
fmt=['%f%f%*s' repmat('%f',1,10)];
C = textscan(fid,fmt,'Headerlines',30,'Delimiter',';');
Deb = cell2mat(C(:,:));
fclose(fid);
Thanks you !

1 件のコメント

dpb
dpb 2016 年 3 月 20 日
What's the point of not simply wrapping textscan with cell2mat directly instead of the temporary C? Use the 'collectoutput' named parameter to collect like terms, too...

この質問は閉じられています。

製品

質問済み:

2016 年 3 月 19 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by