フィルターのクリア

How to delete 'NA' columns from a text file?

2 ビュー (過去 30 日間)
chocho
chocho 2018 年 4 月 3 日
コメント済み: chocho 2018 年 4 月 3 日
Hi guys, I have a text file which has 7 rows and 499 columns and in some columns, there are 'NA'
How can i delete them?
Example.txt:
NA T2b t1c t3b
60 79 78 50
7 7 9 7
t2c t3a t2c t3b
4 5 3 NA
0.1 0.1 0.18 0.1
4 4 5 3
  2 件のコメント
Akira Agata
Akira Agata 2018 年 4 月 3 日
You mean, you want to replace 'NA' with some value, such as 0? or you want to delete row or column with 'NA'?
chocho
chocho 2018 年 4 月 3 日
I want to delete the whole columns which contain 'NA' like in the attached example column 1 and 4 should be deleted completely

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2018 年 4 月 3 日
編集済み: Stephen23 2018 年 4 月 3 日
This is easy with textscan, strcmp, and some indexing:
opt = {'MultipleDelimsAsOne',true};
fmt = repmat('%s',1,4); % define how many columns!
[fid,msg] = fopen('test.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
C = [C{:}];
idx = any(strcmp(C,'NA'),1);
D = C(:,~idx);
which generates this matrix:
D =
T2b t1c
79 78
7 9
t3a t2c
5 3
0.1 0.18
4 5
The test file is attached to this answer. It is also easy to write this to a new file:
tmp = D';
len = max(cellfun('length',D(:)));
fmt = sprintf('%%-%ds',len+1);
fmt = [repmat(fmt,1,size(D,2)),'\n'];
[fid,msg] = fopen('test_new.txt','wt');
assert(fid>=3,msg)
fprintf(fid,fmt,tmp{:});
fclose(fid);
this generates a new file which looks like this:
T2b t1c
79 78
7 9
t3a t2c
5 3
0.1 0.18
4 5
  1 件のコメント
chocho
chocho 2018 年 4 月 3 日
Great! exactly, what i want.
@Stephen Cobeldick Thanks a lot friend

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2018 年 4 月 3 日
fid = fopen('data.txt','rt') ;
S = textscan(fid,'%s','delimiter','\n') ;
fclose(fid) ;
S = S{1} ;
idx = strfind(S, 'NA');
idx = find((cellfun('isempty', idx)));
S = S(idx)
  1 件のコメント
chocho
chocho 2018 年 4 月 3 日
@KSSV, I have tried your code, it deleted the rows but I want to delete the columns that have NA and not the rows

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by