Read from text file between header and footer
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Brantosaurus
il 16 Ott 2023
Commentato: Star Strider
il 16 Ott 2023
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 Commenti
Risposta accettata
Star Strider
il 16 Ott 2023
Modificato: Star Strider
il 16 Ott 2023
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 Commenti
Star Strider
il 16 Ott 2023
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.
.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!