Convert a Cell of Strings to a Double in Matlab

2020-04-16 03:13发布

How do i convert a Cell as the following:

>> A = [{'2'};{'2'};{'****'};{'23'};{'23.6'}]

A = 

    '2'
    '2'
    '****'
    '23'
    '23.6'  

To a double as the Following

A =

    2.0000
    2.0000
       NaN
   23.0000
   23.6000

2条回答
够拽才男人
2楼-- · 2020-04-16 03:48

str2double can be called directly on a cell array of strings:

>> X = str2double(A)
X =
    2.0000
    2.0000
       NaN
   23.0000
   23.6000

On an unrelated note, the notation used to define the cell array A can be simplified a bit:

>> A = {'2'; '2'; '****'; '23'; '23.6'}
A = 
    '2'
    '2'
    '****'
    '23'
    '23.6'

no need for all those curly brackets :)

查看更多
唯我独甜
3楼-- · 2020-04-16 04:03

Use the function str2double on each entry of the cell array like this:

cellfun(@str2double, A)
查看更多
登录 后发表回答