Find and extract column values from a text file
Ältere Kommentare anzeigen
Hi all,
I have a txt file and a part of it, it's like this below:
==============================================================================================================
Allestimento: Nr. 1 1.5 GSE T4 DOHC DDCT MHEV (P2 48V) LONGITUDE FWD EMEA Base 215/65 R16
Accessori :
==============================================================================================================
========================== = PESI = ============================ = ASSETTI = ===========================
condizione di carico asse asse asse K scuotim fle ant freqPro W scuotim fle pos freqPro beccheg
ant[kg] pos[kg] tot[kg] [mm] ant[mm] [mm/100kg] ant[Hz] [mm] pos[mm] [mm/100kg] pos[Hz] [gradi]
---------------------- ------- ------- ------- ------- ------- ---------- ------- ------- ------- ---------- ------- --------
Standard 0 887 528 1415 -14.7 8 36.4 1.3 -21.6 -13.8 44.3 1.58 0° 9'
Standard A 894 555 1449 -13.6 9.1 36.2 1.3 -15.6 -7.8 44.2 1.54 0° 2'
Assetto di Disegno 844 590 1434 -22.7 0 37.2 1.33 -7.8 0 43.9 1.49 0°-19'
Teorico di Progetto 973 685 1658 0.7 23.4 35.3 1.26 12.8 20.6 43.1 1.38 0°-16'
1 Persone + 0kg 927 591 1518 -7.5 15.2 35.9 1.28 -7.6 0.2 43.9 1.49 0° 0'
2 Persone + 0kg 960 628 1588 -1.6 21.1 35.3 1.27 0.4 8.2 43.6 1.44 0°-2'
3 Persone + 30kg 969 719 1688 0 22.7 35.2 1.26 20 27.8 42.8 1.35 0°-26'
5 Persone + 50kg 992 856 1848 4 26.7 35.1 1.25 40.2 48 17.4 1.92 0°-48'
4 Persone + 30kg 982 776 1758 2.2 24.9 35.4 1.25 32.3 40.1 42.2 1.3 0°-40'
0 Persone + 147kg 947 649 1596 -4 18.7 35.9 1.27 4.9 12.7 43.4 1.42 0°-12'
This txt file is "periodic", so this kind of table is present more than one time with different values. From this file, I have to extract the last column ('beccheg', see the underlined values) of all of these tables and put them in differents arrays (to be then sorted and to let me take only some values from them, always periodically: in particular the 'Standard A' value).
I started like this:
clear all;
fid = fopen('520MCA.lis','r');
text = textscan(fid,'%s','Delimiter','');
text = text{1};
fid = fclose(fid);
beccheggio = regexp(text,'Standard A[\s\.=]+(\d+){2}[\s\.=]+(\d+){3}[\s\.=]+(\d+){4}[\s\.=]+[-]+(\d+)','tokens')
beccheggio = [beccheggio{:}];
beccheggio = str2double([beccheggio{:}]).';
Then I blocked myself because of two reasons:
1) the regular expression to be matched, to reach the last value of the 'Standard A' row of each table, could be too long, complicated and not equal for each table
2) there is not just one row, within the period of the file, which started with 'Standard A'.
I hope someone can help me.
Thanks,
Mattia
Akzeptierte Antwort
Weitere Antworten (1)
Mathieu NOE
am 13 Okt. 2020
hello
see below :
fid = fopen('data.txt');
tline = fgetl(fid);
k = 0;
start_line = 9;
while ischar(tline)
k = k+1;
if k > start_line-1 % the extraction start when row index is 9
[m,n] = size(tline);
ind_stop = n;
ind_start = ind_stop-8; % the beccheg value must be written on max 8 character length (taken from the length of "--------" line above
beccheg_string = tline(ind_start:ind_stop);
% find index of "°" mark
ind_deg_separator = findstr(beccheg_string,'°');
Deg = str2num(beccheg_string(1:ind_deg_separator-1));
Minutes = str2num(beccheg_string(1+ind_deg_separator:end-1));
% conversion to degrees (D/M/S => deg
degrees(k+1-start_line,:) = Deg+Minutes/60;
end
tline = fgetl(fid);
end
fclose(fid);
degrees % left uncommented to check values in command window
%save or export as ascii or csv file : degrees
7 Kommentare
Gloria Longo
am 14 Okt. 2020
Bearbeitet: Gloria Longo
am 14 Okt. 2020
Mathieu NOE
am 14 Okt. 2020
Hello Mattia
no problem
could you please send me a couple of your original data txt files - maybe the error is due I copy paste your txt from this matlab central page
Ciao
Gloria Longo
am 14 Okt. 2020
Gloria Longo
am 14 Okt. 2020
Mathieu NOE
am 14 Okt. 2020
OK
could you try again to send me your data files - but change the extension to txt first so that it will not be rejected by the Matlab portal
Mathieu NOE
am 14 Okt. 2020
sorry, I have no acces to google drive at the office - blocked by internet security firewall
if it's not too big you can send it per email at : mathieu.noe@hutchinson.com
Gloria Longo
am 14 Okt. 2020
Kategorien
Mehr zu Language Support finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!