How to replace string by double in cell array

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

Would it be possible for you to upload the file for testing?
Here is the file with some example columns. The deficient data is in column cc.

댓글을 달려면 로그인하십시오.

 채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020년 5월 19일

0 개 추천

D1D=readtable('test.xlsx');
D1D.cc=str2double(D1D.cc);

댓글 수: 2

if you need to remove NaN's, use:
Index=isnan(D1D.cc);
D1D.cc(Index)=0;
% similarly for other columns ...
Thank you. This works great!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020년 5월 19일

0 개 추천

use a command: double()
To get back your strings use the command: char()

댓글 수: 1

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일

0 개 추천

filename = 'test.xlsx';
opt = detectImportOptions(filename);
opt = setvaropts(opt, 3, 'Type', 'double');
T = readtable(filename, opt);

댓글 수: 5

Gregory Wurtz
Gregory Wurtz 2020년 5월 19일
편집: Gregory Wurtz 2020년 5월 19일
This works if T is a table, but in my case T is loaded in a cell array, in which case this code below won't work. Thanks
filename = 'test.xlsx';
opt = detectImportOptions(filename);
opt = setvaropts(opt, 3, 'Type', 'double');
T{1} = readtable(filename, opt);
your question specifically said that it was loaded into a table.
You can always use table2cell if you need a cell array.
the accepted answer is for a table() as well.
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
Thanks Walter.

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2020a

태그

Community Treasure Hunt

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

Start Hunting!

Translated by