Using regexp to capture parts of a filename

13 ビュー (過去 30 日間)
Mary
Mary 2013 年 6 月 10 日
I have a filename that is something like
Exp000_DD2CM000_PN000_block1_predecision
I need to extract different parts of this filename and use them to determine which path for a looping if statement to take.
For example:
I need it to load the file determine that file = Exp000_DD2CM000_PN000_block1_predecision experimentName = DD2CM000 participantName = PN000 block= 1 type = pre or post
if type = pre
...
elseif type = post
... code
end
right now I'm using
>> experimentName =regexp(str,'DD2CM(/d*)','match')
without any luck.
any ideas?
- ML

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 10 日
編集済み: Azzi Abdelmalek 2013 年 6 月 10 日
x='Exp000_DD2CM000_PN000_block1_predecision'
a=regexp(x,'_','split')
experimentName = a{2}
participantName = a{3}
block=regexp(a{4},'\d*')
type = a{5}
  1 件のコメント
Mary
Mary 2013 年 6 月 10 日
this works wonderfully - Thanks!

サインインしてコメントする。

その他の回答 (2 件)

Jonathan Sullivan
Jonathan Sullivan 2013 年 6 月 10 日
Your slash is the wrong way. Try:
experimentName =regexp(str,'DD2CM(\d*)','match')
  1 件のコメント
Mary
Mary 2013 年 6 月 10 日
my bad - I made this error while retyping. Thank you for the reply!

サインインしてコメントする。


Daniel Shub
Daniel Shub 2013 年 6 月 10 日
編集済み: Daniel Shub 2013 年 6 月 10 日
Depending on how structured your file names are, it might be easier to skip the regexp part.
file = 'Exp000_DD2CM000_PN000_block1_predecision';
x = strfind(file, '_');
f = @(n,m,x)x((m(n)+1):(m(n+1)-1));
experimentName = f(2, [0, x, length(file)], file);
participantName = f(3, [0, x, length(file)], file);
block = f(4, [0, x, length(file)], file);
type = f(5, [0, x, length(file)], file);
Then you just want to throw out parts of each variable
experimentName(6:end)
participantName(3:end)
block(6:end)
type(1:3)
The structure of your data might more naturally fit
experimentName(end-2:end)
participantName(end-2:end)
block(end)
type(1:3)
  1 件のコメント
Mary
Mary 2013 年 6 月 10 日
I'll give this a try! - Thank you :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by