Unable to concatenate the table variables 'Var1' and 'Var2', because their types are double and cell.

283 次查看(过去 30 天)
Hello everybody and happy new year!!
I'm trying to import a .csv file in Matlab. My code sobstitutes NaN values with 9999 and then tries to concatenate data from 34x13 table to 31x12 matrix, skipping the last 3 rows.
clear; clc;
p = readtable('Arrone-pluviometro(36045)-2019-0-24','ReadVariableNames',false);
isnum = varfun(@isnumeric,p,'output','uniform');
fillVal = repmat({''},size(isnum));
fillVal(isnum) = {9999};
p = fillmissing(p,'Constant',fillVal) % Fill the missing data
p = p{1:31,:};
I get this error:
Unable to concatenate the table variables 'Var1' and 'Var2', because their types are double and cell.
My .csv is attached here.
Thanks in advance!

回答(2 个)

Cris LaPierre
Cris LaPierre 2021-1-3
编辑:Cris LaPierre 2021-1-3
You are trying to extract all the data to an array. In an array, all the data must be of the same type. The error is because your first column is doubles and your second is char. This is because readtable automatically detected the "521*" in row 34 of column2 and has set the datatype to be char.
If your goal is to have the data be in a matrix, I would suggest using readmatrix. You set up the read options appropriately to read in the desired range and fill the missing values with 9999. By making it 31x12, I assume you want to remove the first column? Note that the output here is missing the multiplier on the matrix values.
opts = detectImportOptions("Arrone-pluviometro(36045)-2019-0-24.csv","Range","B1");
opts = setvartype(opts,"double");
opts.MissingRule = 'fill';
opts = setvaropts(opts,'FillValue',9999);
opts.DataLines = [2 32];
p = readmatrix('Arrone-pluviometro(36045)-2019-0-24.csv',opts)
p = 31×12
9.9990 9.9990 9.9990 9.9990 9.9990 9.9990 0 9.9990 9.9990 9.9990 0.0014 0.0002 9.9990 9.9990 9.9990 9.9990 9.9990 0 0 9.9990 9.9990 0.0098 0.0102 0.0104 9.9990 9.9990 9.9990 9.9990 9.9990 0 0 9.9990 9.9990 0.0096 0.0620 0.0022 9.9990 9.9990 9.9990 9.9990 9.9990 0 0 9.9990 9.9990 0 0.0104 0 9.9990 9.9990 9.9990 9.9990 9.9990 0 0 9.9990 9.9990 0 0.0218 0 9.9990 9.9990 9.9990 9.9990 9.9990 0 0 9.9990 9.9990 0.0022 0.0020 0.0004 9.9990 9.9990 9.9990 9.9990 9.9990 0 0 9.9990 9.9990 0.0002 0.0002 0.0026 9.9990 9.9990 9.9990 9.9990 9.9990 0 0 9.9990 9.9990 0 0.0306 0 9.9990 9.9990 9.9990 9.9990 9.9990 0 0 9.9990 9.9990 0.0002 0.0008 0.0006 9.9990 9.9990 9.9990 9.9990 9.9990 0 0.0078 9.9990 9.9990 0 0.0002 0
However, my preference would be to keep your data as a table. You can use this page to learn how to access data in a table.
pT = readtable('Arrone-pluviometro(36045)-2019-0-24.csv',opts)
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
pT = 31x12 table
Gen_ Feb_ Mar_ Apr_ Mag_ Giu_ Lug_ Ago_ Set_ Ott_ Nov_ Dic_ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ 9999 9999 9999 9999 9999 9999 0 9999 9999 9999 1.4 0.2 9999 9999 9999 9999 9999 0 0 9999 9999 9.8 10.2 10.4 9999 9999 9999 9999 9999 0 0 9999 9999 9.6 62 2.2 9999 9999 9999 9999 9999 0 0 9999 9999 0 10.4 0 9999 9999 9999 9999 9999 0 0 9999 9999 0 21.8 0 9999 9999 9999 9999 9999 0 0 9999 9999 2.2 2 0.4 9999 9999 9999 9999 9999 0 0 9999 9999 0.2 0.2 2.6 9999 9999 9999 9999 9999 0 0 9999 9999 0 30.6 0 9999 9999 9999 9999 9999 0 0 9999 9999 0.2 0.8 0.6 9999 9999 9999 9999 9999 0 7.8 9999 9999 0 0.2 0 9999 9999 9999 9999 9999 0 0 9999 9999 0 13.2 0 9999 9999 9999 9999 9999 0 1.8 9999 9999 0.2 6.6 1 9999 9999 9999 9999 9999 0 0.2 9999 9999 0 3 17.8 9999 9999 9999 9999 9999 0 0 9999 9999 0.2 0.2 0.2 9999 9999 9999 9999 9999 0.2 0.8 9999 9999 9.6 40 0.2 9999 9999 9999 9999 9999 0 0 9999 9999 0 5.8 0

David Hill
David Hill 2021-1-3
Why not just readmatrix? What column are you skipping?
p = readmatrix('Arrone-pluviometro(36045)-2019-0-24');
p(isnan(p))=9999;
p=p(1:31,:);

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by