필터 지우기
필터 지우기

Convert cell array with different datatypes to numeric

조회 수: 5 (최근 30일)
Christian F.
Christian F. 2015년 3월 11일
편집: Christian F. 2015년 3월 11일
Hi everybody,
I am dealing with a cell array, where single numbers are saved as strings in the cells and some cells are empty. So cell2mat does not work. I want to convert the vector to double while keeping the numbers and replacing the empty cells with NaN.
What would be the fastest way to do this?
Best

채택된 답변

Stephen23
Stephen23 2015년 3월 11일
편집: Stephen23 2015년 3월 11일
As you only have "single numbers" in the cells, then you can use str2double directly on the cell array:
>> A = {'123',[],'4','5678',[],'90'};
>> str2double(A)
ans = [123,NaN,4,5678,NaN,90]
The documentation states: "If str does not represent a valid scalar value, str2double returns NaN."
  댓글 수: 3
Stephen23
Stephen23 2015년 3월 11일
My answer uses str2double, not str2num.
Christian F.
Christian F. 2015년 3월 11일
편집: Christian F. 2015년 3월 11일
thanks, it works

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by