dlmread does not read last row txt file
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2024-10-10
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,:)
回答(1 个)
Prateekshya
2024-10-10
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 个评论
Stephen23
2024-10-10
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!