Reading csv file NaN and NaT problem
42 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi all,
I am trying to make a csv file read.File is located in "file path". The Variable Names is in line 2.
detectImportOptions(filepath);
opts.DataLines=[1,Inf];
T = readtable(filepath,opts);
I can read the file but the problem is that some of the variables are changing to NaT or NaN, is it possible to set on line 2 (where the variable names are located) to character before executing readable function?
0 comentarios
Respuestas (3)
Jeremy Hughes
el 31 de Oct. de 2018
Based on the description, I think this is what you'd want. Without an attached file, I cannot say more.
opts = detectImportOptions(filepath);
opts.VariableNamesLine = 2;
opts.DataLines=[3,Inf];
T = readtable(filepath,opts,'ReadVariableNames',true);
0 comentarios
dpb
el 31 de Oct. de 2018
detectImportOptions(filepath);
You didn't save the output of detectImportOptions so you don't have an import options object at this point.
Hence,
opts.DataLines=[1,Inf];
simply creates the one element in the struct named opts that you passed to readtable but everything else is its default, NOT what the detectImportOptions function would have divined to be the characteristics of the file.
Try
opts=detectImportOptions(filepath);
T = readtable(filepath,opts);
first before resorting to anything more exotic; generally the automagic scanning routines are able to infer "who's who in the zoo" without human intervention.
You can use setvartype, yes, if it turns out to be needed as well as setting the specific time format string, but try the above first.
5 comentarios
SKG
el 31 de Oct. de 2018
1 comentario
Jeremy Hughes
el 31 de Oct. de 2018
Note, this will set all the data you import to be cells of character vectors, no numbers.
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!