How to extract two type of data from text without losing indexing?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
dpb
el 17 de Oct. de 2022
Editada: dpb
el 17 de Oct. de 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]
2 comentarios
dpb
el 17 de Oct. de 2022
Editada: dpb
el 17 de Oct. de 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...
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!