dlmread does not read last row txt file

4 Ansichten (letzte 30 Tage)
Ruwan
Ruwan am 4 Jan. 2014
Kommentiert: Stephen23 am 10 Okt. 2024
Hi,
I have the following odd problem when using dlmread.
i have a .txt file containing following tab delimited data:
3 3.7 4.2 4.60579 4.7 5.10579 5.2 6.62317 7
10 3925.19 3977.6 4015.03 4045.41 4052.46 4082.84 4089.89 4196.43 4245.61
17 8432.7 7663.48 7114.04 7166.84 7179.1 7231.9 7250.05 8878.22 8960.18
17 8432.7 7081.23 7114.04 7159.8 7170.92 7231.9 7250.05 8878.22 8960.18
25.1472 13678.9 12235.8 11205 11278.6 11295.6 11369.2 11395.1 14327.3 14447.4
25.1472 13678.9 11159.1 11205 11268.7 11283.8 11369.2 11395.1 14327.3 14447.4
27 14872 13309.5 12193.5 12271.7 12289.8 12368 12395.6 15566.5 15695.3
27 14872 12144.6 12193.5 12261.1 12277 12368 12395.6 15566.5 15695.3
83.3251 51141.4 51546.5 51835.9 52070.7 52125.3 52360.1 52414.6 53238.2 53630.8
100 65811 66321.6 66686.3 66982.3 67051.1 67347.1 67415.8 68453.9 68919.5
when i use
dlmread(filename,'\t',0,0);
it reads everything except the last row (hence up till the row that starts with 83.3251) unless i add an extra empty row to the file, then it actually reads everything up till the row that starts with 100.
Is there any explanation for this behaviour and/or a way to solve this issue? Except printing an empty row? If other people are to create their own files, they might not know of this issue and wonder why their input files are not read correctly...
  1 Kommentar
Stephen23
Stephen23 am 10 Okt. 2024
Unfortunately the OP did not upload the data file.
Tested on this forum:
format long G
M = dlmread('mytest.txt','\t',0,0);
M(end,:)
ans = 1×10
1.0e+00 * 100 65811 66321.6 66686.3 66982.3 67051.1 67347.1 67415.8 68453.9 68919.5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Prateekshya
Prateekshya am 10 Okt. 2024
Hello Ruwan,
The issue you are facing with dlmread in MATLAB might be due to the way the function handles end-of-file (EOF) markers or newline characters in text files. If the last line of your file does not end with a newline character, dlmread might not recognize it as a complete line, leading to it being ignored.
Here are a few ways to address this issue:
  • Ensure Proper File Ending: Manually ensure that your text file ends with a newline character. You can do this by opening the file in a text editor and pressing Enter at the end of the last line to insert a newline.
  • Use readmatrix or readtable: If you are using a newer version of MATLAB (R2019a and later), consider using readmatrix or readtable, which provide more robust handling of text files.
data = readmatrix(filename, 'Delimiter', '\t');
Please find the documentation links for these functions:
  • Modify the File Programmatically: If you want to ensure that files created by others are read correctly, you can programmatically append a newline character at the end of the file before reading it.
% Open the file in append mode and add a newline if necessary
fid = fopen(filename, 'a');
fprintf(fid, '\n');
fclose(fid);
% Now read the file
data = dlmread(filename, '\t', 0, 0);
  • Check File Size: Before reading the file, check if the last character is a newline and append one if it's not. This can be done using file I/O functions.
I hope this helps!
  1 Kommentar
Stephen23
Stephen23 am 10 Okt. 2024
"The issue you are facing with dlmread in MATLAB might be due to the way the function handles end-of-file (EOF) markers or newline characters in text files. If the last line of your file does not end with a newline character, dlmread might not recognize it as a complete line, leading to it being ignored."
DLMREAD does not require a trailing EOL/EOF character.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by