While writing my code, I realized that the first 3 rows from a table I had recently written to a .txt file were missing. I had created the table in a Matlab code file and wanted to use it in a different Matlab code file, so I used writetable(). I then used readtable() in the other code file.
After looking through all lines of code in which I made any modifications to the table, I concluded that my problem was being caused directly by the readtable() function. I then wrote a completely separate test code to see if my suspicion was correct, and it was.
Here is my test code:
a = {"30.03.20","20:36","20.5.44.45";"30.03.20","20:36","text";"30.03.20","20:36","text";"30.03.20","20:36","03/30/20"};
a = cell2table(a)
writetable(a)
opts = detectImportOptions("a.txt");
opts = setvartype(opts,'a1','string');
opts = setvartype(opts,'a2','string');
opts = setvartype(opts,'a3','string');
readtable('a.txt',opts)
With this test code I saw that the output of the readtable('a.txt')
line was indeed missing a few rows. I will add a screenshot of the outputs below.
Now, I have narrowed down the problem. I just don't know how to solve it.
The weirdest part of all this is that in my original two codes I have two more tables being written and then read, and they do not seem to be experiencing this issue.
I am currently messing with the options in the detectImportOptions object to see if I can solve the issue. However, if someone has experienced this, and/or knows what is causing the readtable() function to skip the first three lines of my table, it would really help me out.
These are the outputs both of the original table, and the table produced by the readtable() function
You are setting the options wrong. First, the default configuration would work returning a table of cells:
readtable('a.txt')
However, if you want to specify additional options use the correct settings:
opts = delimitedTextImportOptions('NumVariables',3,'VariableNamesLine',1,'DataLines',2);
Actually, the keyword you are looking for is 'DataLines'
, specifying the first line, where data occurs. The other keywords are good practice -- you'll just get a different naming of the columns if you miss both keywords 'NumVariables'
and 'VariableNamesLine'
(the first automatically numbers the column names, while the second uses what is written in the first row... in your case both are the same). You get the same result as above.
If you want to have a specific data type use the key 'VariableTypes'
:
opts = delimitedTextImportOptions('NumVariables',3,'VariableNamesLine',1,'DataLines',2,'VariableTypes',{'string','string','string'});
readtable('a.txt',opts)
Now, why didn't your code work correctly?
Well, you cannot detect in which lines the data starts. Adding opts.DataLines = 2;
to your code solves your problem.