How to Text Scan and remove columns full of zeros?

2 ビュー (過去 30 日間)
Ibro Tutic
Ibro Tutic 2017 年 5 月 15 日
回答済み: Andrei Bobrov 2017 年 5 月 15 日
I am scanning in large data sets and many of them have multiple fields that don't have data (recorded as 0). How would I use text scan to get the data into Matlab and remove any columns with 0s and move all columns over to not leave empty space in the resulting array? I attached a sample text file (tab delimited). The columns with zeros are not always the same, I need to somehow dynamically figure out which columns have 0s and remove them (the entire column needs to be removed, including the header)

採用された回答

Cam Salzberger
Cam Salzberger 2017 年 5 月 15 日
Hello Ibro,
I'd highly recommend "readtable" over "textscan" in this case. This will also allow you to keep track of which columns are which, since tables keep header names. Otherwise, how would you know which columns were removed, and what are the columns that are left?
T = readtable('sample.txt');
for k = size(T,2):-1:1
if ~any(T{:,k})
T(:,k) = [];
end
end
I went backwards so that if I removed a variable, it wouldn't skip checking the next one, or require weird while loop syntax. There are other ways to access the different variables in a table, but this was one of the easiest to do while in a loop.
-Cam
  2 件のコメント
Ibro Tutic
Ibro Tutic 2017 年 5 月 15 日
I'm getting an error that says Undefined function 'any' for input arguments of type 'cell'.
Ibro Tutic
Ibro Tutic 2017 年 5 月 15 日
Got it to work, just added a tab delimiter.

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 5 月 15 日
T = T(:,any(T{:,:}));

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by