フィルターのクリア

How to extract only part of a file name

25 ビュー (過去 30 日間)
Inês Mendes
Inês Mendes 2015 年 5 月 19 日
コメント済み: Inês Mendes 2015 年 5 月 19 日
I am trying to extract a file but i don´t want to extract it´s entire name. For example:
In
inv_1000061014358_20150424_115235
I only want to extract inv_1000061014358.
Can anyone help?
Thanks!!!!

採用された回答

dpb
dpb 2015 年 5 月 19 日
編集済み: dpb 2015 年 5 月 19 日
Presuming the numeric substring isn't the same length always,
ix=strfind(s,'_'); % get the underscore locations
t=s(1:ix(2)-1); % return the substring up to 2nd underscore
One can also use find to only get the first two locations; would be handy if it also had the ability to return an N th argument instead of just first or last N; then wouldn't have to use the intermediate index vector.
Or, one can use regexp
  1 件のコメント
Inês Mendes
Inês Mendes 2015 年 5 月 19 日
Thank you so much!:)

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

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 5 月 19 日
編集済み: Guillaume 2015 年 5 月 19 日
In this sort of cases, I would use a regular expression just because it expresses better that you want to extract a portion of the string that conform to a particular pattern.
It's not obvious from the question what that pattern is. Assuming it is: 1 or more alphabetic characters, an underscode, one or more numbers, before another underscore, then:
s = 'inv_1000061014358_20150424_115235';
ss = regexp(s, '^[a-z]+_\d+(?=_)', 'match', 'once')
If regular expressions look too daunting then dpb's answer is the way to go.
  1 件のコメント
Inês Mendes
Inês Mendes 2015 年 5 月 19 日
Thanks!Both options work fine :)

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by