CSV readtable dosen't work
11 views (last 30 days)
Show older comments
Dominik Coenen
on 9 Aug 2023
Commented: Dominik Coenen
on 11 Aug 2023
Hi there,
I am trying to read a CSV file with readtable. Readtable doesn't detect the correct datatype and detectImportOptions also dosen`t work either. I tried to convert the last columns to numbers with the following loop:
for i = 11:(width(stgsa_csv_table))
stgsa_csv_table{:,i} = str2double(stgsa_csv_table{:,i});
end
But it's not possible and following error message is displayed :
Error using {}
Conversion to cell from double is not possible.
Does anyone know how to solve this problem?
Thanks Dominik
4 Comments
Stephen23
on 11 Aug 2023
F = '02.08.23_15.54.32_RTCM_STA8100_D0_stgsa.csv';
T = readtable(F, 'Delimiter',',', 'ExpectedNumVariables',26, 'VariableNamesLine',1)
Accepted Answer
Star Strider
on 9 Aug 2023
Perhaps something like this —
T1 = cell2table(compose('%f',randn(5,7)))
VN = T1.Properties.VariableNames;
T2 = varfun(@str2double,T1);
T2.Properties.VariableNames = VN
.
2 Comments
Star Strider
on 10 Aug 2023
As always, my pleasure!
One approach (consistent with the current approach) —
T1 = cell2table(compose('%f',randn(5,7)));
T1.HexVar = {'8';'9';'A';'B';'C'}
VN = T1.Properties.VariableNames;
T1.HexVar = compose('%f',hex2dec(T1.HexVar))
T2 = varfun(@str2double,T1);
T2.Properties.VariableNames = VN
This requires knowing what variables are hex and specifically converting them to numeric first. The detectImportOptions documentation under Parameters for Text Files Only has HexType that governs how to convert it. I get the impression that the converstion to numeric is done automatically, although selecting 'text' woiuld prevent its conversion so it would be read into the table as text.
.
More Answers (0)
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!