Read from text file between header and footer
8 次查看(过去 30 天)
显示 更早的评论
Is there an easy way to read numerical data from a text file below a 3 line header and above a footer starting with a sequence of asterisks. The data is in 3 complete columns of unknown length. Delimiter and asterisks can be modified if necessary.
Tried readtable but footer is causing problems as there doesn't appear to be an option from detectImportOptions.
see attached for example data file
ld dim real
ratio ht speed
[] [s] [m/s]
5.0000000e-01 3.4497665e+02 2.1712214e+03
1.0000000e+00 3.8184070e+02 2.3923590e+03
1.5000000e+00 3.9916530e+02 2.4919967e+03
2.0000000e+00 4.0763144e+02 2.5342191e+03
2.5000000e+00 4.1118036e+02 2.5473084e+03
...
**********************************************
2.8682373e+00 4.1181815e+02
2.6080322e+00 2.5476604e+03
0 个评论
采纳的回答
Star Strider
2023-10-16
编辑:Star Strider
2023-10-16
type('data.txt')
fidi = fopen('data.txt','rt')
C = textscan(fidi, '%f%f%f', 'HeaderLines',3, 'CollectOutput',1)
fclose(fidi);
A = cell2mat(C)
figure
plot(A(:,1), A(:,[2 3]))
grid
It will stop automatically at the line of asterisks, however if you want to re-start it to read the last two lines, that is an option.
.
4 个评论
Star Strider
2023-10-16
Thank you.
The fseek call moves to the next position in the file, beyond the non-numeric text tthat stopped textscan.
If you have more of these files, use readtable with fixedWidthImportOptions, as I did here, in the last part of my previous Comment. That includes inserting the NaN value in the blanks.
Reprising that section here —
opts = fixedWidthImportOptions('NumVariables',3, 'VariableWidths',[15 16 16], 'DataLines',[4 25; 27 28]);
T1 = readtable('data.txt', opts)
T1(end-1:end,:)
% missing = varfun(@ismissing, T1);
% missing(end-1:end,:)
T2 = varfun(@(x)fillmissing(x,'constant',{'NaN'}), T1)
T2(end-1:end,:)
format longG
A = str2double(table2array(T2))
A(end-1:end,:)
You would need to manually keep track of the number of lines after the asterisks line (here the last two), and make approipriate changes to the 'NumVariables', 'VariableWidths', and 'DataLines' name-value pairs in the fixedWidthImportOptions call, however that should be straightforward.
I no longer have access to R2017a, and while there used to be online documentation for a few previous releases, that appears to no long be an option. Everything I have listed here should be available in — and compatible with — R2017a, however I cannot check to be certain.
.
更多回答(0 个)
另请参阅
类别
在 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!