How to extract two type of data from text without losing indexing?

3 visualizzazioni (ultimi 30 giorni)
I have .txt data from sensors in the following format:
I wanted to import x, y data (as highlighted in green) and the parametric data (highlighted in blue).
I was able to exract separately using the following code
Imp= importdata('Test.txt') ;
Imp(1,:) = [];
TF = contains(Imp,"0 0"); % for extracting the parametric data
S = Imp(TF); %This option only stores the above mentioned data, thus losing the indexing.
I made empty columns for the parameters
cols = length(S);
Para1 = zeros(cols,1); %Parameter 1
Para2 = zeros(cols,1); %Parameter 2
Para3 = zeros(cols,1); %Parameter 3
...
Then I ran the loop to put the specific values into those arrays
for i = 1:cols
sample=S(i);
sample = extractAfter(sample,"*");
sample = extractAfter(sample,":");
sample = extractAfter(sample,":");
values = string(sample);
values = str2num(values);
Para1(i) = values(3);
Para2(i) = values(4);
Para3(i) = values(5);
end
I used the same method to find the x and y values. But it gave me 2 tables with no data on index numbers. This way I lose the indexing of the original data. Because later I want to relate these specific parameters to the corresponding x,y coordinates.
My idea is to somehow make the GP# rows equal to zero instead of [], so that later I can trace back the index numbers. I cannot achieve that. In the end result I want to have a table which has the data in this form (table made in excel).
Any help in this will be much appreciated. I am also attaching the text.txt file.
~thanks.
  2 Commenti
KSSV
KSSV il 17 Ott 2022
Modificato: KSSV il 17 Ott 2022
It is suggested to attach a sample text file, so that we can give a try. Have a look on regexp.
UH
UH il 17 Ott 2022
I have attached the text file. I am looking into regexp. Thanks

Accedi per commentare.

Risposta accettata

dpb
dpb il 17 Ott 2022
Modificato: dpb il 17 Ott 2022
file=readlines(websave('Test.txt','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1159058/Test.txt'));
file(1)=strrep(file(1),"SIG STRNGTH","SIG-STRNGTH"); % fixup bad naming issue in file
hdr=split(file(1)).'; hdr=hdr(strlength(hdr)>0);
file=file(2:end); % strip header, keep rest
ixGp=contains(file,"Gp#"); % indices each group start
xy=str2double(split(extractBetween(file(ixGp),'x,y =',', q'),','));
data=split(file(~ixGp));
tT=array2table(data(:,3:end));
tT=convertvars(tT,tT.Properties.VariableNames(1),'duration');
tT=convertvars(tT,tT.Properties.VariableNames(2:end),'double');
You can now do as you please on the construction of the final table as far as variable names for the columns from the header record or your own.
My suggestion then as far as synchronizing the xy data would be to add X, Y as variables to the table and spread each value for the group across the records associated with it rather than keeping them separate or trying to build a spreadsheet-like represenation as your example. This provides the ability to use grouping variables or select by ranges or whatever directly within the table and makes everything regular.
You can do this by using the ixGp variable indices to count the number in each group via
nGp=diff(find(ixGp))-1; % -1 to account for the header record
to convert the logical addressing vector into the locations in the original file; the difference between which will be the number of each group. The sample file has three each; that may/may not??? be true in general.
xy=cell2mat(arrayfun(@(i)kron(xy(i,:),ones(nGp(i),1)),[1:numel(nGp)].','uni',0));
tT=[array2table(xy,'variablenames',{'X','Y'}) tT]
tT = 12×21 table
X Y Time PARA1 CH RISE COUN ENER DURATION AMP A-FRQ RMS ASL PCNTS THR R-FRQ I-FRQ SIG-STRNGTH ABS-ENERGY FRQ-C P-FRQ _____ _____ ________ _______ __ ____ ____ ____ ________ ___ _____ ___ ___ _____ ___ _____ _____ ___________ __________ _____ _____ 1384 76.59 00:00:31 -0.0049 7 81 35 17 557 61 63 0 18 6 45 60 74 1.0828e+05 4200 150 102 1384 76.59 00:00:31 -0.0049 6 76 35 15 505 62 69 0 20 8 45 62 105 98500 3826 140 97 1384 76.59 00:00:31 -0.0049 5 34 4 0 44 48 91 0 9 3 45 111 88 4654 71.398 178 97 1103 193.8 00:02:44 -0.0037 5 89 72 63 875 72 82 0 7 13 45 75 146 3.9444e+05 48438 178 102 1103 193.8 00:02:44 -0.0037 6 68 85 61 807 70 105 0 8 9 45 102 132 3.8069e+05 38998 146 97 1103 193.8 00:02:44 -0.0037 4 98 60 19 638 59 94 0 8 11 45 90 112 1.2187e+05 4114 170 166 936.9 111.7 00:02:50 -0.0015 4 61 38 16 388 63 98 0 17 7 45 95 114 1.0433e+05 5565 182 102 936.9 111.7 00:02:50 -0.0015 5 31 21 8 389 58 54 0 17 4 45 47 129 54979 1618 157 92 936.9 111.7 00:02:50 -0.0003 3 103 22 8 342 57 64 0 21 10 45 50 97 52506 1338 164 102 916.4 128.8 00:02:52 -0.0027 4 38 23 6 268 60 86 0 11 6 45 74 157 43581 1453 182 146 916.4 128.8 00:02:52 -0.0024 5 43 11 3 140 51 79 0 10 4 45 72 93 18706 353.64 164 92 916.4 128.8 00:02:52 -0.0024 3 6 6 2 83 50 72 0 13 1 45 64 166 12947 274.03 161 97
  2 Commenti
dpb
dpb il 17 Ott 2022
Modificato: dpb il 17 Ott 2022
ADDENDUM:
You could easily parse more of the group record variables and do the same thing with them along with x,y...along with keeping/removing any of those not needed/wanted in the above by selective addressing in the array2table step or cleaning out after...
UH
UH il 19 Ott 2022
Thank you for your thorough analysisa and answer.
I tried achieving it using expedience, as data were around 16k rows.
I simply used "contains" function and found the indices of the string. In those indices, I replaced the whole text with similar layout except all values were zero (did the same for GP type). It achieved the results.
Import(1,:) = [];
n=length(Import);
String_Para = contains(Import,"Gp");
StringCoord = contains(Import,"0 0");
ImportCoord = Import;
Import(String_Para) = {'* 0 00:00:00.000000 -0.0000 0 00 00 00 000 00 00 0.0000 0 00 00 00 000 000.000 00.000 000 000'};
ImportCoord(StringCoord) = {'* Gp# 0[0,0,0] x,y = 0, 0, q = 0 dT[0, 0] Src Amplitude = 0.0'};
S = Import;
S2 = ImportCoord;
The method you explained is more organized. I will implement it in my work. Thank you.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by