Reading csv file NaN and NaT problem

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?

답변 (3개)

Jeremy Hughes
Jeremy Hughes 2018년 10월 31일

0 개 추천

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);
dpb
dpb 2018년 10월 31일

0 개 추천

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

SKG
SKG 2018년 10월 31일
Actually I made a mistake when I was writing the question. I already tried that. It is not working. It is not taking the second line's all variables as characters.
dpb
dpb 2018년 10월 31일
Well, show us a small section of the input file...
Jeremy Hughes
Jeremy Hughes 2018년 10월 31일
There's no way to have one row be text and the others numeric. Tables are homogeneous in the "row" direction. This is why the fields are being converted to NaN or NaT.
Try my answer above and see if that gives you closer to what you expect.
SKG
SKG 2018년 10월 31일
I already tried, it is not working
dpb
dpb 2018년 10월 31일
편집: dpb 2018년 10월 31일
ONE MORE TIME!!!!
We can't help you if you won't show us what the input data format actually is.
Attach a few lines of the data file that illustrates the format and where you have a problem.

댓글을 달려면 로그인하십시오.

SKG
SKG 2018년 10월 31일

0 개 추천

Solved it:
opts = detectImportOptions(filepath); opts.DataLines=[1,Inf ]; opts.VariableTypes(1, 1:end) = {'char'};

댓글 수: 1

Jeremy Hughes
Jeremy Hughes 2018년 10월 31일
Note, this will set all the data you import to be cells of character vectors, no numbers.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

질문:

SKG
2018년 10월 31일

댓글:

2018년 10월 31일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by