How to Delete Non-Numeric Columns from Table

18 ビュー (過去 30 日間)
Kevin
Kevin 2014 年 7 月 29 日
コメント済み: Kevin 2014 年 7 月 30 日
I'm using readtable to load data from a text file. Some columns have text in them; others don't. I want to effectively delete the columns that have at least one entry of non-numeric data, so that I can convert the remaining table to an array (using table2array).
How can I do this?
  1 件のコメント
Andrew Reibold
Andrew Reibold 2014 年 7 月 29 日
編集済み: Andrew Reibold 2014 年 7 月 29 日
Can you please provide an example of the readtable output? :-)

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

採用された回答

Arun Mathew Iype
Arun Mathew Iype 2014 年 7 月 29 日
I think it is better to use the “Import Data” tool which allows you to import data as an array directly using a GUI and also to generate scripts for doing the same everytime.
However to answer your question the following steps and example should be helpful.
  1. Create a TEXT file called “sampledata.txt” and copy the following data into it.
LastName,Gender,Age,Height,Weight,Smoker Smith,M,38,71,176,1 Johnson,M,43,69,163,0 Williams,F,38,64,131,0 Jones,F,S,67,133,0 Brown,F,49,64,119,0
  1. Run the below script to delete the columns which have text in them and generate the required array. Note that if even one item in a column is a string then the whole column is of cell array
% read the data into a variable
T = readtable('sampledata.txt')
V = T.Properties.VariableNames;
for i = [1:width(T)]
v_is_cell(i) = iscell(T.(V{i}));
end
%use logical indexing to delete the required columns
T(:,v_is_cell) = [];
table2array(T)
T =
LastName Gender Age Height Weight Smoker
__________ ______ ____ ______ ______ ______
'Smith' 'M' '38' 71 176 1
'Johnson' 'M' '43' 69 163 0
'Williams' 'F' '38' 64 131 0
'Jones' 'F' 'S' 67 133 0
'Brown' 'F' '49' 64 119 0
ans =
71 176 1
69 163 0
64 131 0
67 133 0
64 119 0
  1 件のコメント
Kevin
Kevin 2014 年 7 月 30 日
This is what I was looking for. Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by