extract numbers from cell array

149 ビュー (過去 30 日間)
alex
alex 2014 年 5 月 10 日
コメント済み: Sebastian Lopez 2023 年 4 月 19 日
hello!
i have this cell array wich may have many cells,and not only three as here.
A = {'jdldi'; 'jdks5.4555h'; 'f67'}
i want to extract only the numbers,and show them as one number
for this example i want the result to be 5.455567
any help please?
Thank you very much!
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 10 日
What about 67?
alex
alex 2014 年 5 月 10 日
in the first cell:no numbers
in the second cell:5.4555
in the third cell:67
so,i want to merge all the numbers from every cell and take the number 5.455567

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 10 日
A = {'jdldi'; 'jdks5.4555h'; 'f67'}
b=regexp(A,'\d+(\.)?(\d+)?','match')
out=str2double([b{:}])
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 10 日
編集済み: Azzi Abdelmalek 2014 年 5 月 10 日
A = {'jdldi'; 'jdks5.4555h'; 'f67'}
b=regexp(A,'\d+(\.)?(\d+)?','match')
out=strjoin([b{:}],'')
alex
alex 2014 年 5 月 10 日
thank you very much Azzi! have a nice day!

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

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2014 年 5 月 10 日
A1 = regexp(A,'[\d*\.]*\d*','match')
A2 = [A1{:}]
out = str2double(strcat(A2{:}))

Jan
Jan 2014 年 5 月 10 日
編集済み: Jan 2014 年 5 月 18 日
A simple version:
A = {'jdldi'; 'jdks5.4555h'; 'f67'}
S = cat(2, A{:});
S(isletter(S)) = [];
The simpler the code, the less chances to insert a bug.
  1 件のコメント
Sebastian Lopez
Sebastian Lopez 2023 年 4 月 19 日
Thanks Jan! I wasn't aware of the isletter function

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by