Unrecognized variable when using delimitedTextImportOptions
4 次查看(过去 30 天)
显示 更早的评论
I followed the example in the documentation for the command "delimitedTextImportOptions" to import from a file with a variable that I explicitly named "fecha":
varNames = ["ccaa_iso","fecha","num_casos","num_casos_prueba_pcr","num_casos_prueba_test_ac","num_casos_prueba_otras","num_casos_prueba_desconocida"];
varTypes = ["char","datetime", "int32", "int32", "int32", "int32", "int32" ];
delimiter = ",";
dataStartLine = 2;
extraColRule = "ignore";
opts = delimitedTextImportOptions("VariableNames", varNames, "VariableTypes", varTypes, "Delimiter", delimiter, "ExtraColumnsRule", extraColRule);
datosccaas26122020 = readtable("datos_ccaas_26-12-2020.csv", opts);
But then when I try to use the following command:
dies = datestr(fecha, "yyyymmdd");
it throws an error, telling me that fecha is not a recognized variable. But if I simply right-click on the .csv file and use the GUI to import the data, it then can read the variable fecha. So what's happening here?
2 个评论
Stephen23
2022-10-29
Forcing meta-data (i.e. the timestamp) into the variable name is unlikely to be a good approach:
datosccaas26122020 = readtable(..)
Much better would be to use a name without meta-data in it, e.g.:
tbl = readtable("datos_ccaas_26-12-2020.csv", opts);
dies = datestr(tbl.fecha, "yyyymmdd");
采纳的回答
Voss
2022-10-29
fecha would be a variable (i.e., a column) in the table datosccaas26122020, so the syntax would be:
dies = datestr(datosccaas26122020.fecha, "yyyymmdd");
3 个评论
Campion Loong
2022-11-17
On separate note, as the table variable "fecha" is a datetime, datestr is not recommended for turning it into text any more. You can use either char or string (i.e. note the upper case "M" versus lower case "m" - datetime conforms to the Unicode standard):
dies = string(datosccaas26122020.fecha, "yyyyMMdd");
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Other Formats 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!