help with reading txt file into matlab

조회 수: 3 (최근 30일)
Teshan Rezel
Teshan Rezel 2021년 8월 26일
댓글: Ive J 2021년 8월 26일
hi folks,
I have a very large txt file that I want to read into Matlab. When I use the "import data" option it reads the file just fine, but when using "import selection", it gives me a single filed with "NaN". I've tried exporting as a function, live script and script as well but it gives the same results.
Can anyone help me with this please?
opts = delimitedTextImportOptions("NumVariables", 6, "Encoding", "UTF16-LE");
% Specify range and delimiter
opts.DataLines = [48, 48];
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["CLOSED", "Var2", "Var3", "Var4", "Var5", "Var6"];
opts.SelectedVariableNames = "CLOSED";
opts.VariableTypes = ["double", "string", "string", "string", "string", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["Var2", "Var3", "Var4", "Var5", "Var6"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var2", "Var3", "Var4", "Var5", "Var6"], "EmptyFieldRule", "auto");
% Import the data
T = readtable(pathVar, opts);
  댓글 수: 3
Teshan Rezel
Teshan Rezel 2021년 8월 26일
@Ive J that works, thank you! please can you post this as an answer so I can accept it?
Ive J
Ive J 2021년 8월 26일
My pleasure, but I guess you should accept @dpb answer, it's more detailed and informative also for future questions.

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

채택된 답변

dpb
dpb 2021년 8월 26일
Be far easier if you attached a representative piece of the file, but...
The above is the wrong way to go about creating in import options object generally -- it's possible to manage to set the needed parameters individually this way, but highly error-prone besides being a lot of effort.
Observations on the above--
  1. opts.DataLines = [48, 48]; % this says there's only one line of data in the file to be read; clearly strongly at odds with the prior description of a "very large" file.
  2. opts.SelectedVariableNames = "CLOSED"; % then this says to read only one of the six variables and ignore the others
The combination of those would not be terribly surprising doesn't work as expected.
As @Ive J suggests, the first thing to do is just use readtable without any options and see if the file is regular-enough for it to successfully read on its own -- that importdata was successful strongly implies you won't need any special treatment at all. I would note that his comment is backwards to the input of 'NumHeaderLines',0 as this tells readtable the first record in the file is data. That was undoubtedly just a typo, intending 'NumHeaderLines',1 instead.
But, that also isn't consistent with your explicit use of 48 in 1. above that would imply quite a large header section at the beginning of the file. Again, without the file, we simply can't tell.
If you have a release of R2019b or later, then readtable on its own does all the input scanning that detectImportOptions does; only if you have an earlier release or there is still something not recognized correctly after that should there be any need at all.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by