MATLAB Answers

help with reading txt file into matlab

1 view (last 30 days)
Teshan Rezel
Teshan Rezel on 26 Aug 2021
Commented: Ive J on 26 Aug 2021
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 Comments
Ive J
Ive J on 26 Aug 2021
My pleasure, but I guess you should accept @dpb answer, it's more detailed and informative also for future questions.

Sign in to comment.

Answers (1)

dpb
dpb on 26 Aug 2021
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.

Community Treasure Hunt

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

Start Hunting!