CSV readtable dosen't work

11 visualizaciones (últimos 30 días)
Dominik Coenen
Dominik Coenen el 9 de Ag. de 2023
Comentada: Dominik Coenen el 11 de Ag. de 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 comentarios
Stephen23
Stephen23 el 11 de Ag. de 2023
F = '02.08.23_15.54.32_RTCM_STA8100_D0_stgsa.csv';
T = readtable(F, 'Delimiter',',', 'ExpectedNumVariables',26, 'VariableNamesLine',1)
T = 4475×13 table
tow gnss_id mode fix_mode band_mask hdop vdop pdop sync sat_mask sat01 sat02 sat03 __________ _______ ____ ________ _________ ____ ____ ____ ____ _________ _____ _____ _____ 3.0929e+05 0 1 3 1 2.9 2.7 4 1 4e+07 2 3 31 3.0929e+05 2 1 3 1 2.9 2.7 4 1 0 NaN NaN NaN 3.0929e+05 3 1 3 1 2.9 2.7 4 1 0 NaN NaN NaN 3.0929e+05 7 1 3 1 2.9 2.7 4 1 8.002e+08 22 36 NaN 3.0929e+05 13 1 3 1 2.9 2.7 4 0 20 46 NaN NaN 3.0929e+05 0 1 3 1 2.9 2.7 4 1 4e+07 2 3 31 3.0929e+05 2 1 3 1 2.9 2.7 4 1 0 NaN NaN NaN 3.0929e+05 3 1 3 1 2.9 2.7 4 1 0 NaN NaN NaN 3.0929e+05 7 1 3 1 2.9 2.7 4 1 8.002e+08 22 36 NaN 3.0929e+05 13 1 3 1 2.9 2.7 4 0 20 46 NaN NaN 3.0929e+05 0 1 3 1 2.9 2.7 4 1 4e+07 2 3 31 3.0929e+05 2 1 3 1 2.9 2.7 4 1 0 NaN NaN NaN 3.0929e+05 3 1 3 1 2.9 2.7 4 1 0 NaN NaN NaN 3.0929e+05 7 1 3 1 2.9 2.7 4 1 8.002e+08 22 36 NaN 3.0929e+05 13 1 3 1 2.9 2.7 4 0 20 46 NaN NaN 3.0929e+05 0 1 3 1 2.9 2.7 4 1 4e+07 2 3 31
Dominik Coenen
Dominik Coenen el 11 de Ag. de 2023
That's weird. The same line of code gives me a different result and data types are not recognized.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 9 de Ag. de 2023
Perhaps something like this —
T1 = cell2table(compose('%f',randn(5,7)))
T1 = 5×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 _____________ _____________ _____________ _____________ _____________ _____________ _____________ {'0.737553' } {'0.737513' } {'-0.122106'} {'1.469730' } {'-0.879202'} {'1.394899' } {'-0.660295'} {'2.355983' } {'0.407245' } {'1.367768' } {'1.489333' } {'-1.308921'} {'0.754867' } {'-0.736306'} {'1.689538' } {'2.381704' } {'-0.961434'} {'-0.736418'} {'-0.119951'} {'0.653452' } {'-0.209794'} {'0.473317' } {'-0.035799'} {'-0.680802'} {'1.090041' } {'-0.158576'} {'-1.444836'} {'-1.575928'} {'-0.430204'} {'1.775675' } {'2.057153' } {'0.120885' } {'0.270623' } {'-0.592076'} {'-1.926796'}
VN = T1.Properties.VariableNames;
T2 = varfun(@str2double,T1);
T2.Properties.VariableNames = VN
T2 = 5×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 _______ _________ ________ ________ ________ ________ ________ 0.73755 0.73751 -0.12211 1.4697 -0.8792 1.3949 -0.66029 2.356 0.40725 1.3678 1.4893 -1.3089 0.75487 -0.73631 1.6895 2.3817 -0.96143 -0.73642 -0.11995 0.65345 -0.20979 0.47332 -0.035799 -0.6808 1.09 -0.15858 -1.4448 -1.5759 -0.4302 1.7757 2.0572 0.12089 0.27062 -0.59208 -1.9268
.
  2 comentarios
Dominik Coenen
Dominik Coenen el 10 de Ag. de 2023
Editada: Dominik Coenen el 10 de Ag. de 2023
Thanks. This works.
In my case one column is a hex number. Is it possible to convert this certain column to hex?
Star Strider
Star Strider el 10 de Ag. de 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'}
T1 = 5×8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 HexVar _____________ _____________ _____________ _____________ _____________ _____________ _____________ ______ {'0.307080' } {'1.332698' } {'0.452805' } {'0.763319' } {'-1.798578'} {'1.854305' } {'-0.486706'} {'8'} {'-1.020499'} {'1.740624' } {'-0.198948'} {'0.244900' } {'0.116841' } {'-0.539302'} {'-0.093323'} {'9'} {'-2.086324'} {'0.330824' } {'-2.041314'} {'-0.064818'} {'0.020421' } {'2.119804' } {'0.276070' } {'A'} {'0.407898' } {'-1.075031'} {'0.652554' } {'-0.965714'} {'-1.015894'} {'-1.602972'} {'1.615435' } {'B'} {'-0.175186'} {'2.857849' } {'-0.991484'} {'1.188328' } {'1.524303' } {'-0.039499'} {'-0.034836'} {'C'}
VN = T1.Properties.VariableNames;
T1.HexVar = compose('%f',hex2dec(T1.HexVar))
T1 = 5×8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 HexVar _____________ _____________ _____________ _____________ _____________ _____________ _____________ _____________ {'0.307080' } {'1.332698' } {'0.452805' } {'0.763319' } {'-1.798578'} {'1.854305' } {'-0.486706'} {'8.000000' } {'-1.020499'} {'1.740624' } {'-0.198948'} {'0.244900' } {'0.116841' } {'-0.539302'} {'-0.093323'} {'9.000000' } {'-2.086324'} {'0.330824' } {'-2.041314'} {'-0.064818'} {'0.020421' } {'2.119804' } {'0.276070' } {'10.000000'} {'0.407898' } {'-1.075031'} {'0.652554' } {'-0.965714'} {'-1.015894'} {'-1.602972'} {'1.615435' } {'11.000000'} {'-0.175186'} {'2.857849' } {'-0.991484'} {'1.188328' } {'1.524303' } {'-0.039499'} {'-0.034836'} {'12.000000'}
T2 = varfun(@str2double,T1);
T2.Properties.VariableNames = VN
T2 = 5×8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 HexVar ________ _______ ________ _________ ________ _________ _________ ______ 0.30708 1.3327 0.45281 0.76332 -1.7986 1.8543 -0.48671 8 -1.0205 1.7406 -0.19895 0.2449 0.11684 -0.5393 -0.093323 9 -2.0863 0.33082 -2.0413 -0.064818 0.020421 2.1198 0.27607 10 0.4079 -1.075 0.65255 -0.96571 -1.0159 -1.603 1.6154 11 -0.17519 2.8578 -0.99148 1.1883 1.5243 -0.039499 -0.034836 12
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.
.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by