how to convert cell array to numeric array?

4 views (last 30 days)
Suresh R
Suresh R on 3 Dec 2021
Commented: Stephen on 3 Dec 2021
how to convert cell array of size (4514,1) to numeric array where each cell has values like 'fh8453655' and some cells are nan.
  4 Comments
Jan
Jan on 3 Dec 2021
In other words: fh8453655 is not a number. It is not clear, what you expect as output. Perhaps 8453655?

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 3 Dec 2021
maybe this ?
>> a = {'fh8453655'}
>> beginStr = regexp(a, '\d+', 'match');
>> beginNumbers = cellfun(@(x) str2double(x{1}), beginStr)
beginNumbers =
8453655
  1 Comment
Stephen
Stephen on 3 Dec 2021
Simpler, but equally fragile:
a = {'fh8453655'}
a = 1×1 cell array
{'fh8453655'}
b = str2double(regexprep(a,'\D+',''))
b = 8453655
b = str2double(regexp(a,'\d+','match','once'))
b = 8453655

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!