![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/477803/image.png)
Unable to concatenate the table variables 'Var1' and 'Var2', because their types are double and cell.
189 次查看(过去 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!
0 个评论
回答(2 个)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/477803/image.png)
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)
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)
0 个评论
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,:);
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!