MATLAB: Importdata can only import a few lines of a .txt file

importdataMATLAB

Hello everyone,
I am using importdata to import data from a .txt file:
filename = '123.txt';
data = importdata(filename);
data = data.data;
  • The .txt file is as attached. I used Excel to check the .txt file and it shows 24755 lines of data in addition to 1 line of header text.
  • However, importdata can only import the first 66 lines.
  • I also tried to use the "Import Data" button on the MATLAB panel and it worked fine. Nevertheless, I need to process a large number of .txt files and I certainly need to use code instead of clicking the "Import Data" button for hundreds of times.
It seems something wrong when using this importdata function, but I could not find out why up to now. Can anyone help me figure it out?
Thanks!
Sincerely,
Junfei Tang

Best Answer

importdata() cannot handle 1.#QNAN0e+000 .
filename = '123.txt';
opt = detectImportOptions(filename);
The above only needs to be done once for each group of files that have the same headers. After that you can loop over the files,
data = readtable(filename, opt);
This will give you a table() object that has, for example,
time V_Inverter_Phase_A_Inverter_Phase_B_ V_Rectifier_Phase_A_Rectifier_Phase_B_ V_Rectifier_Positive_Rectifier_Negative_ I_L_exc_ I_R_tr_1_ I_R_tr_2_
____________________ ____________________________________ ______________________________________ ________________________________________ ________ _________ _________
1.39487416755957e-09 48.60414 0.01123047 -1.181915 2.381693 -2.161491 0.5377774
1.39488942618726e-09 48.60414 0.01122113 -1.181929 2.381693 -2.16149 0.5379625
1.39489002293214e-09 48.60414 0.01147461 -1.181885 2.381693 -2.16149 0.5383048
2.39489002240578e-09 48.66073 0.01171875 -1.181641 2.381693 -2.111191 0.5515398
2.51989002580944e-09 48.66698 0.01171875 -1.181641 2.381693 -2.104903 0.5532185
2.76989002567785e-09 48.67606 0.01171875 -1.181641 2.381693 -2.092325 0.5566501
3.26989002541467e-09 48.686 0.01184082 -1.181641 NaN -2.067164 0.5635144
4.15460238434306e-09 48.69257 0.01226807 -1.181824 NaN -2.02264 0.5756616
4.27171845118757e-09 48.6927 0.0123291 -1.181885 2.381692 -2.016746 0.5772697
You can refer to individual columns. Or if you want all of the columns together as a numeric array, then it would be
data{:,:}
Related Question