Extrapolate a specific group of numbers and letters from a "char" variable.

2 views (last 30 days)
Hello everyone, I would like to extrapolate a specific group of numbers and letters from a "char" variable. Maybe this problem has already been solved but I couldn't find an answer anywhere.
My original char variable is:
20upg13 D112-A104 FH08547 P800 L0600@10000 a.csv
and I want to extrapolate "P800" and "L0600". By using the "strtok" command, I get what I want but if the variable is like this:
20upg13 P19 D112-A104 L02 FH08547 P800 L0600@10000 a.csv
my method doesn't work anymore.
I would like to know how do I tell Matlab to extrapolate the letter P followed by exactly three numbers (which can vary) and do the same for the letter L.
I attach the code I've used below. Thank you very much.
% code
P = '\w*P\w*';
L = '\w*L\w*';
for i=1:nfiles
a = strrep(filelist(i).name,'_',' ');
aa = strsplit(a);
[token_P remain_P] = strtok(a, P);
pp = strtok(remain_P);
pp = strrep(pp,'P','');
[token_L remain_L] = strtok(a, L);
ll = strrep(remain_L,'@',' ');
ll = strtok(ll);
ll = strrep(ll,'L0','');
Outnames = aa;
Pres = pp;
Lift = ll;
end

Accepted Answer

Stephen23
Stephen23 on 11 Feb 2016
>> fmt = '.*(P\d{3}).*(L\d{4}).*';
>> regexp('20upg13 D112-A104 FH08547 P800 L0600@10000 a.csv',fmt,'tokens','once')
ans =
'P800' 'L0600'
>> regexp('20upg13 P19 D112-A104 L02 FH08547 P800 L0600@10000 a.csv',fmt,'tokens','once')
ans =
'P800' 'L0600'

More Answers (1)

Walter Roberson
Walter Roberson on 11 Feb 2016
regexp(STRING, 'P\d\d\d', 'match')

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!