Unable to concatenate the table variables 'Var1' and 'Var2', because their types are double and cell.
198 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Cris LaPierre
on 3 Jan 2021
Edited: Cris LaPierre
on 3 Jan 2021
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)
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 Comments
David Hill
on 3 Jan 2021
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 Comments
See Also
Categories
Find more on Logical 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!