Conversion of cell element to double

3 ビュー (過去 30 日間)
Digvijay Rawat
Digvijay Rawat 2015 年 12 月 28 日
コメント済み: John Manalo 2020 年 5 月 28 日
Hello.
I want to convert a cell element to double array element. I know the command to do this is str2double but the problem is that the cell element has a number along with a letter that I want to omit. I will try to explain it through the following example.
Cell element: '123456789 a'
Double element (by using str2double): NaN
I want to retain just the numeric part of the cell element. So I would want 123456789 as the double element in the above example. Can anyone please help as to how to go about this?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 12 月 28 日
a={'123456789 a' '455'}
b=regexp(a,'\d+','match')
c=cellfun(@(x) str2double(x{1}),b)
  3 件のコメント
Digvijay Rawat
Digvijay Rawat 2015 年 12 月 28 日
Hey I noticed right now that this piece of code works only for integer values, it simply omits decimal places. I tried changing the /d to /g and /f but then it gave an error. Could you please suggest what to do if the values are in decimals?
John Manalo
John Manalo 2020 年 5 月 28 日
just use cell2mat function

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

その他の回答 (1 件)

Jan
Jan 2015 年 12 月 28 日
Or:
a = {'123456789 a' '455'}
c = cellfun(@(x) sscanf(x{1}, '%g', 1))

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by