Unrecognized variable when using delimitedTextImportOptions
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mikel Cotillas
am 29 Okt. 2022
Kommentiert: Campion Loong
am 17 Nov. 2022
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 Kommentare
Stephen23
am 29 Okt. 2022
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");
Akzeptierte Antwort
Voss
am 29 Okt. 2022
fecha would be a variable (i.e., a column) in the table datosccaas26122020, so the syntax would be:
dies = datestr(datosccaas26122020.fecha, "yyyymmdd");
3 Kommentare
Campion Loong
am 17 Nov. 2022
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");
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Other Formats finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!