Find and extract column values from a text file

3 Ansichten (letzte 30 Tage)
Gloria Longo
Gloria Longo am 13 Okt. 2020
Kommentiert: Mathieu NOE am 15 Okt. 2020
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 9'
Standard A 894 555 1449 -13.6 9.1 36.2 1.3 -15.6 -7.8 44.2 1.54 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

Mathieu NOE
Mathieu NOE am 14 Okt. 2020
So , hopefully now a solution that works
I put the excel output file as well, that includes min and max values for beccheg already computed
now I hope that the file structure will not change too much...
i prefered to stick with low level string functions. I asume another "expert" will find out that there are alternatives with the latest matlab versions. For my fun I still develop for R12 backward compatibility (if possible)
enjoy
  4 Kommentare
Mathieu NOE
Mathieu NOE am 14 Okt. 2020
Hello Mattia
glad it works !
ok I think the "upgrades" are doable without much hard work. I'll have a look tomorrow
please send me on my email a couple of other data files so I can test the new code
ciao
Mathieu NOE
Mathieu NOE am 15 Okt. 2020
And now the final version !
enjoy !
I have further refined the program and even simplified it a bit .
So the main script is : assetti1E.m
And it calls the function : retrieve_data.m
The major change is that I do not longer need my “stop line” calculation in the first portion of the code
If there is important thinks to know is to see the lines 96 to 98 in retrieve_data.m
% job for beccheg data retrieval
nb_lines_max = 20; % assuming table of data will never exceed this size
offset_start = 4;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Mathieu NOE
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
Mathieu NOE
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
Gloria Longo am 14 Okt. 2020
Hello Mathieu,
I tried to change the extension but this didn't solve the problem: I just sent you an email with the file attached, this comes from schianom@hotmail.it.
Thanks a lot,
Mattia

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Language Support finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by