Round in a cell array that also contains string

Hello everyone,
I am trying to round figures in a cell array however in that same array I also have string types. round changes the value of the string therfore I can't use formulas like
cellfun(@(x)round(x,N),c)
I mean I don't how to integrate `isnumeric` to this formula.
Is there a fomula that knows to round only numeric number?
Thank you in advance.

 채택된 답변

Stephen23
Stephen23 2019년 7월 24일
편집: Stephen23 2019년 7월 24일

0 개 추천

idx = cellfun(@isnumeric,C);
C(idx) = cellfun(@(x)round(x,N),C(idx),'uni',0)

댓글 수: 3

Thank you for your answer!
I get a
Conversion to cell from double is not possible.
from the second line..
@Jack Arnoldi: you are right, I forgot the 'UniformOutput',false. I have fixed my answer:
>> C = {1.234,'cat';'hat',5.6789}
C =
[1.234] 'cat'
'hat' [5.6789]
>> idx = cellfun(@isnumeric,C);
>> C(idx) = cellfun(@(x)round(x,2),C(idx),'uni',0)
C =
[1.23] 'cat'
'hat' [5.68]
Thank you that works perfectly!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2019년 7월 24일

댓글:

2019년 7월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by