Round in a cell array that also contains string

7 Ansichten (letzte 30 Tage)
Jack Arnoldi
Jack Arnoldi am 24 Jul. 2019
Kommentiert: Jack Arnoldi am 24 Jul. 2019
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Jul. 2019
Bearbeitet: Stephen23 am 24 Jul. 2019
idx = cellfun(@isnumeric,C);
C(idx) = cellfun(@(x)round(x,N),C(idx),'uni',0)
  3 Kommentare
Stephen23
Stephen23 am 24 Jul. 2019
@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]
Jack Arnoldi
Jack Arnoldi am 24 Jul. 2019
Thank you that works perfectly!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by