How to assign a double value to a cell array?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
My goal is to replace any missing values in my cell array T1 with -999, if the column is composed of floating numbers, and replace any missing values with an empty string like ' ', if the column is composed of text strings?
Below is my code. Unfortunately, I got an error saying: "Conversion to cell from double is not possible."
% Replace the remaining missing values with -999:
missingIndices2 = ismissing(string(T1));
T1(missingIndices2) = -999;
% convert the cell into a table:
Table = cell2table(T1);
0 commentaires
Réponse acceptée
Voss
le 10 Mar 2025
Modifié(e) : Voss
le 10 Mar 2025
Put curly braces {} around the -999, which makes the right-hand side of the assignment a scalar cell array:
T1(missingIndices2) = {-999};
8 commentaires
Voss
le 10 Mar 2025
Modifié(e) : Voss
le 10 Mar 2025
"why are the output NaNs"
Because your cell array has some cells that contain scalar numerics, some cells that contain <missing>s, and some cells that contain (empty) character vectors.
If you use ismissing(string(C)) as the condition to replace with {-999}, then you replace the <missing>s but not the empty character vectors because string('') is "" and "" is not <missing>.
ismissing(string(''))
If you use ~cellfun(@isscalar,C) as the condition, then you replace the empty character vectors but not the <missing>s because <missing> is a scalar
isscalar(missing)
To replace the contents of any cell that contains <missing> or a non-scalar array, which seems like what you are trying to do [EDIT: not really, thanks for clarifying], you can use some condition like cellfun(@(x)~isscalar(x) || ismissing(x),C)
C = load('test.mat').T1;
rows = [99:108, 576:585];
C(rows,:)
C(cellfun(@(x)~isscalar(x) || ismissing(x),C)) = {-999};
T = cell2table(C);
C(rows,:)
T(rows,:)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!