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
Walter Roberson
2020년 5월 19일
Would it be possible for you to upload the file for testing?
Gregory Wurtz
2020년 5월 19일
채택된 답변
추가 답변 (2개)
Sulaymon Eshkabilov
2020년 5월 19일
0 개 추천
use a command: double()
To get back your strings use the command: char()
Walter Roberson
2020년 5월 19일
filename = 'test.xlsx';
opt = detectImportOptions(filename);
opt = setvaropts(opt, 3, 'Type', 'double');
T = readtable(filename, opt);
댓글 수: 5
Gregory Wurtz
2020년 5월 19일
편집: Gregory Wurtz
2020년 5월 19일
Walter Roberson
2020년 5월 19일
your question specifically said that it was loaded into a table.
You can always use table2cell if you need a cell array.
Walter Roberson
2020년 5월 19일
the accepted answer is for a table() as well.
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
2020년 5월 20일
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!