extract decimal number from a file name

3 ビュー (過去 30 日間)
Ayman Mounir
Ayman Mounir 2021 年 1 月 4 日
コメント済み: Ayman Mounir 2021 年 1 月 6 日
Hello everyone,
Firwstly, Happy new year.
I have have a file name such as '2C2C', sometimes the number has a fraction for example '2.5C3C' or 2.5C3.5C'. I need to exract these number at all cases (with fraction and without).
Thanks in advance

採用された回答

Star Strider
Star Strider 2021 年 1 月 4 日
Try this:
filnam = {'2C2C', '2.5C3.5C'};
nrs = cellfun(@(x)sscanf(x, '%fC%fC'), filnam, 'Unif',0)
Result = cell2mat(nrs).'
producing:
Result =
2 2
2.5 3.5
.
  2 件のコメント
Ayman Mounir
Ayman Mounir 2021 年 1 月 4 日
Thanks I appreciate it.
but i meant the file name would be 'NNN_2.5C3C_BlaBla'. So i ahve to extract the part which has 2.5C3C first.
Star Strider
Star Strider 2021 年 1 月 4 日
This is turning out to be a moving target!
Try this:
filnam = {'NNN_2.5C3C_BlaBla'; 'NNN_2.5C3.5C_BlaBla'};
nrs = regexp(filnam, '(\d.\d)|\d', 'match')
nrs{1}, nrs{2}
producing:
nrs =
2×1 cell array
{1×2 cell}
{1×2 cell}
ans =
1×2 cell array
{'2.5'} {'3'}
ans =
1×2 cell array
{'2.5'} {'3.5'}
.

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

その他の回答 (2 件)

KSSV
KSSV 2021 年 1 月 4 日
str = '2C2C' ;
str = '2.5C3C' ;
% str = '2.5C3.5C' ;
idx = strfind(str,'C') ;
num1 = str2num(str(1:idx(1)-1))
num2 = str2num(str(idx(1)+1:idx(2)-1))
  1 件のコメント
Ayman Mounir
Ayman Mounir 2021 年 1 月 4 日
Thanks I appreciate it.
but i meant the file name would be 'NNN_2.5C3C_BlaBla'. So i ahve to extract the part which has 2.5C3C first.

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


Stephen23
Stephen23 2021 年 1 月 4 日
The most efficient solution by far:
C = {'NNN_2.5C3C_BlaBla'; 'NNN_2.5C3.5C_BlaBla'};
M = sscanf([C{:}],'%*[^_]_%fC%fC_',[2,Inf]).'
M = 2×2
2.5000 3.0000 2.5000 3.5000
  1 件のコメント
Ayman Mounir
Ayman Mounir 2021 年 1 月 6 日
Thanks It works, and happy new year.

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by