Convert cell array with different datatypes to numeric
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Christian F.
am 11 Mär. 2015
Bearbeitet: Christian F.
am 11 Mär. 2015
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
0 Kommentare
Akzeptierte Antwort
Stephen23
am 11 Mär. 2015
Bearbeitet: Stephen23
am 11 Mär. 2015
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!