How to convert strings in a table to numbers?

18 ビュー (過去 30 日間)
Matthew Gordon
Matthew Gordon 2017 年 11 月 10 日
コメント済み: Matthew Gordon 2017 年 11 月 11 日
I have a large table (280,000 x 40) in which some of the columns are numeric but stored as strings, for example:
Block Lot District
'121' '35' '13'
'121' '36' '13'
'121' '37' '13'
Don't ask me why they are stored this way - the original data is in a .txt file. Is there either
  • a) a way to import this file so that the numbers don't show up as strings (currently I'm using readtable) or
  • b) a way to quickly convert all these entries into numbers. Right now I'm using the following code, which seems to work, but takes a long time:
for i=1:40
b1(:,i)=table2array(bk13(:,i));
b2(:,i)=str2double(b1(:,i));
b3(:,i)=array2table(b2(:,i));
bk13{:,i}=table2cell(b3(:,i));
end

採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 11 日
編集済み: Walter Roberson 2017 年 11 月 11 日
It might help to specify the TreatAsEmpty option to readtable, and perhaps the Format option as well.
Using detectImportOptions() and passing the result to readtable() might help.

その他の回答 (1 件)

Jan
Jan 2017 年 11 月 11 日
A simplification of your code omitting unnecessary steps:
for i=1:40
bk13.(i) = str2double(bk13{:,i});
end
  1 件のコメント
Matthew Gordon
Matthew Gordon 2017 年 11 月 11 日
Thanks, this worked for b, but the answer to a worked even better.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by