How to replace string by double in cell array

1 回表示 (過去 30 日間)
Gregory Wurtz
Gregory Wurtz 2020 年 5 月 19 日
コメント済み: Gregory Wurtz 2020 年 5 月 20 日
I am importing an Excel file in Matlab using the readtable command. For some reason, one collumn in the dataset does not load as doubles (all number columns are identically formatted in the Excel file as much as I can tell) but as string characters:
prompting the deficient column:
>>T{3,1}{:,14}
ans =1699×1 cell array
I need to replace the strings in T{3,1}{:,14} with doubles.
The following command returns an error:
>> T{3,1}{:,14}=str2double(T{3,1}{:,14})
Conversion to cell from double is not possible.
If is use:
T{3,1}{:,14}=num2cell(str2double(T{3,1}{:,14}))
the content of the column is no longer a double.
Do you have a solution to this (seemingly trivial) problem?
Thanks
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 5 月 19 日
Would it be possible for you to upload the file for testing?
Gregory Wurtz
Gregory Wurtz 2020 年 5 月 19 日
Here is the file with some example columns. The deficient data is in column cc.

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

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 5 月 19 日
D1D=readtable('test.xlsx');
D1D.cc=str2double(D1D.cc);
  2 件のコメント
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 5 月 19 日
if you need to remove NaN's, use:
Index=isnan(D1D.cc);
D1D.cc(Index)=0;
% similarly for other columns ...
Gregory Wurtz
Gregory Wurtz 2020 年 5 月 19 日
Thank you. This works great!

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

その他の回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 5 月 19 日
use a command: double()
To get back your strings use the command: char()
  1 件のコメント
Gregory Wurtz
Gregory Wurtz 2020 年 5 月 19 日
I am not sure how this solves my problem: I would like to replace the string in the cell array with doubles. I included the data in the feed above.
Thanks for your help.

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


Walter Roberson
Walter Roberson 2020 年 5 月 19 日
filename = 'test.xlsx';
opt = detectImportOptions(filename);
opt = setvaropts(opt, 3, 'Type', 'double');
T = readtable(filename, opt);
  5 件のコメント
Walter Roberson
Walter Roberson 2020 年 5 月 19 日
I tested on your sample file before posting. This code fixes the readtable so that it loads as numeric in the first place avoiding the cell
Gregory Wurtz
Gregory Wurtz 2020 年 5 月 20 日
Thanks Walter.

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

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by