Find strings within other strings then pull data from that point

dear collegaes, please let me ask your help to find a solution for my data analisys.
I have a P0300.txt file that contains a long strip.
here is a portion of P0300.txt:
59 04 03 01 00 65 01 3E 51 09 00 00 51 1C 00 E1
I need to find sets of data, sets could be 2 or 4 digits.
lets say I need to find 01 3E
DATA = regexp(fileread('P0300.txt'), '\r?\n', 'split')';
>> A = string(DATA);
>> B = strfind(A,'01 3E');
B gives me the position where 01 3E is, in this case B = 19
after this I need to extract the 6 digits on the rigth 51 09 00 <-- this values can change depend on test conditions
Any idea on how can I do that?
your feedback will be highly appreciated

 采纳的回答

filename = 'P0300.txt';
to_find = '01 3E';
str = fileread(filename);
pat = strjoin(repmat({'[\dA-F]{2}'},1,3),' ');
C = regexp(str,[to_find ' (' pat ')'],'tokens');
C = [C{:}]
C = 1x1 cell array
{'51 09 00'}

10 个评论

Voss.
thank you for showing me how to solve it
is it possible to make to_find a list of data sets? like a loop......
for example to_ find = 'E1 54' '01 E3'......
and then define pat = strjoin(repmat({'[\dA-F]{2}'},1,3),' '); for each one?
for 'E1 54'
pat = strjoin(repmat({'[\dA-F]{2}'},1,3),' '); %% 3 becasue I need to get 6 digits to the rigth.
then do the same with '01 E3'.
pat = strjoin(repmat({'[\dA-F]{2}'},1,2),' '); %% 2 becasue I need to get 4 digits to the rigth.
sorry I did not include that in my first questrion, just though about it.
Sure, no problem:
filename = 'P0300.txt';
% now to_find is a cell array with two columns; the first column is
% the character vector to find, and the second column is the number
% of digits to grab afterward (the number of digits must be even)
to_find = { ...
'E1 54', 6; ...
'01 3E', 4; ...
};
str = fileread(filename);
N = size(to_find,1);
results = cell(N,1);
for ii = 1:N
pat = strjoin(repmat({'[\dA-F]{2}'},1,to_find{ii,2}/2),' ');
C = regexp(str,[to_find{ii,1} ' (' pat ')'],'tokens');
results{ii} = [C{:}];
end
results
results = 2x1 cell array
{1x1 cell} {1x1 cell}
results{1}
ans = 1x1 cell array
{'00 12 00'}
results{2}
ans = 1x1 cell array
{'51 09'}
update:
what if the output is and odd number?
to_find = { ...
'E1 54', 3; ...
'01 3E', 5; ...
'51 09', 4; ...
};
I tried to update my self but it's giving me ahard time, please help me here one more time.
filename = 'P0300.txt';
% now to_find is a cell array with two columns; the first column is
% the character vector to find, and the second column is the number
% of digits to grab afterward (the number of digits must be even)
to_find = { ...
'E1 54', 3; ...
'01 3E', 5; ...
'51 09', 4; ...
};
str = fileread(filename);
N = size(to_find,1);
results = cell(N,1);
for ii = 1:N
a = to_find(:,2);
b = cell2mat(a);
idx = rem(b,2) == 0
if idx(N) == 1
pat = strjoin(repmat({'[\dA-F]{2}'},1,to_find{ii,2}/2),' ');
C = regexp(str,[to_find{ii,1} ' (' pat ')'],'tokens');
results{ii} = [C{:}];
else
pat = strjoin(repmat({'[\dA-F]{2}'},1,to_find{ii,2}/1),' ');
C = regexp(str,[to_find{ii,1} ' (' pat ')'],'tokens');
results{ii} = [C{:}];
end
end
So you want to split up a two-digit sequence in that case and just grab the first digit? If so, see the modfied code below:
filename = 'P0300.txt';
% now to_find is a cell array with two columns; the first column is
% the character vector to find, and the second column is the number
% of digits to grab afterward
to_find = { ...
'E1 54', 3; ...
'01 3E', 5; ...
'51 09', 4; ...
};
str = fileread(filename);
N = size(to_find,1);
results = cell(N,1);
for ii = 1:N
n = to_find{ii,2};
pat = strjoin(repmat({'[\dA-F]{2}'},1,floor(n/2)),' ');
if rem(n,2)
pat = [pat ' [\dA-F]'];
end
C = regexp(str,[to_find{ii,1} ' (' pat ')'],'tokens');
results{ii} = [C{:}];
end
results{:}
ans = 1x1 cell array
{'00 1'}
ans = 1x1 cell array
{'51 09 0'}
ans = 1x1 cell array
{'00 00'}
it saved my day!! you're the best thanks a lot

请先登录,再进行评论。

更多回答(2 个)

Hey @A-Rod,
To extract 6 digits following a specific pattern in your text file, you can utilize MATLAB's string manupilation functions such as 'regexp'.
Here is a sample MATLAB script to achieve the same:
fileContent = fileread('P0300.txt');
pattern = '01 3E';
% Use regular expression to find the pattern and extract the following 6 digits
expression = [pattern, '\s+([\dA-F]{2}\s+[\dA-F]{2}\s+[\dA-F]{2})'];
match = regexp(fileContent, expression, 'tokens');
% Check if any match is found
if ~isempty(match)
% Extract the first match (if multiple matches are found)
extractedDigits = match{1}{1};
fprintf('Extracted digits: %s\n', extractedDigits);
else
fprintf('Pattern not found in the file.\n');
end
Extracted digits: 51 09 00
You can refer the following MathWorks documentation for more information:
fileContent = fileread('P0300.txt');
prefix="01 3E";
e=" "+digitsPattern(2);
pattern = prefix+e+e+e;
matches = extractAfter( extract(fileContent,pattern), prefix);

1 个评论

I'm using version 2019 digitsPattern is not available. thanks a lot for your feedback

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by