Hello, I do have the following Code
d = dir('/home/user/Documents/Data*.inp');
names = {d.name}
which gives me an Array of Strings.
names(1) is like = "Data_c11_t3.322111_id01111_Units.inp"
names(2) is like = "Data_c101_t4.32111_id01112_Units.inp"
etc.
Now I want to extract the time, which is in the name after "t". How can I extract the time in such a format?
Any suggestions?

 채택된 답변

KL
KL 2017년 11월 2일
편집: KL 2017년 11월 2일

2 개 추천

regexp would be the best idea, I've no big experiences with it, I'll give it a shot anyway,
names = {'Data_c11_t3.322111_id01111_Units.inp';'Data_c101_t4.32111_id01112_Units.inp'};
t = regexp(names,'[0-9]+\.+[0-9*]+','match')

댓글 수: 4

regexp (regular expressions) is indeed the easiest and best way to solve the problem.
There are a few problems with KL regex. It will extract all numbers in the string not just the one required, the '\.+' part should be just '\.| (only one dot is acceptable), and there's no reason to have a * in the [0-9*].
To be guaranteed to extract the number after the _t, the following regular expression would be much better:
t = regexp(names, (?<=_t)\d+\.\d+', 'match', 'once')
This will extract the expression immediately following '_t' that consists of at least one digit (the \d+'), followed by a dot (the |\.) followed by at least one digit (the second \d+).
There are always many ways to extract something with regular expressions, depending on what you want to allow or disallow. In the examples provided, the following would also work:
t = regexp(names, '(?<=_t)[^_]+', 'match', 'once')
which extract everything following _t up to but not including the next _.
Jos (10584)
Jos (10584) 2017년 11월 3일
regular expressions are indeed very powerful, but are, in my humble opinion, way over the top for such a simple problem :)
Guillaume
Guillaume 2017년 11월 3일
To each their own opinion of course. Regular expressions are designed to extract specific patterns in strings, which, in my opinion is exactly the problem at hand.
Christian Berwanger
Christian Berwanger 2017년 11월 8일
Ok. This worked for me. Thank you. I do have still a question:
Can you please explain me how this worked? I searched up regexp on Mathworks documentation. But honestly I still don't really understand why this one works.
To Guilaume: It extraced only the time. Even I have more numbers in the name. Thats why I am asking how this works as I don't see any reason to just take the numbers from time.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jos (10584)
Jos (10584) 2017년 11월 3일

1 개 추천

Use a sscanf to read the number between the _t and _id, ignoring the varying number after the _c. Use cellfun to apply this to all cells:
names = {'Data_c11_t3.322111_id01111_Units.inp' ; 'Data_c101_t4.32111_id01112_Units.inp'} ;
T = cellfun(@(N) sscanf(N,'Data_c%*d_t%f_id'), names) % "%*d" = the * means skip

댓글 수: 3

Stephen23
Stephen23 2017년 11월 3일
편집: Stephen23 2017년 11월 3일
+1 a very neat solution.
Coco Newton
Coco Newton 2019년 8월 2일
@Jos, this is a great answer
How would I adapt the sscanf formatSpec to extract 130 as a numeric value from this string? I have attached the string array variable that this is example string is taken from.
'Position: (19.59862|1.8|53.84677)Rotation: (0 | 130 | 0)'
Many thanks!
Thanks :-) Something along these lines should work for your input
str = 'Position: (19.59862|1.8|53.84677)Rotation: (0 | 130 | 0)'
A = sscanf(str, 'Position: (%*f|%*f|%*f)Rotation: (%*d | %d | %*d)')

댓글을 달려면 로그인하십시오.

KSSV
KSSV 2017년 11월 2일
편집: KSSV 2017년 11월 2일

0 개 추천

str = 'Data_c11_t3.322111_id01111_Units.inp' ;
idx = strfind(str,'_') ;
str2num(str(idx(2)+2:idx(3)-1))
Using regexp:
str = 'Data_c11_t3.322111_id01111_Units.inp' ;
N = str2num( regexprep( str, {'\D*([\d\.]+\d)[^\d]*', '[^\d\.]*'}, {'$1 ', ' '} ) ) ;
t = N(2)

댓글 수: 3

Now I get the following error: Index exceeds matrix dimensions in the line:
time = str2num(fileName(idx(2)+2:idx(3)-1));
Where
fileName = names(i);
KSSV
KSSV 2017년 11월 2일
What is filename? Can you copy it?
I do have it in a for loop. its like
for i = 1:length(names)
fileName = names(i);
idx = strfind(fileName,'_') ;
time = str2DOUBLLE(fileName(idx(2)+2:idx(3)-1));
end
so filename is in the first iteration "Data_c11_t3.322111_id01111_Units.inp"
in the second iteration: "Data_c101_t4.32111_id01112_Units.inp"
and so on.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

태그

질문:

2017년 11월 2일

댓글:

2019년 8월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by