How to assign a double value to a cell array?

14 views (last 30 days)
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);

Accepted Answer

Voss
Voss on 10 Mar 2025
Edited: Voss on 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 Comments
Voss
Voss on 10 Mar 2025
Edited: Voss on 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(''))
ans = logical
0
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)
ans = logical
1
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,:)
ans = 20x2 cell array
{[33.8186]} {[<missing>]} {[33.8898]} {[ 33.8903]} {[33.9370]} {[ 33.9366]} {[33.9753]} {[ 33.9761]} {[33.9749]} {[<missing>]} {[33.9249]} {[ 33.9261]} {[33.9613]} {[ 33.9631]} {[34.1909]} {[ 34.1920]} {[34.2087]} {[<missing>]} {[34.2238]} {[<missing>]} {[36.3150]} {[ 36.3090]} {[36.3190]} {[ 36.3130]} {[36.3410]} {[ 36.3340]} {[36.2880]} {[ 36.2920]} {[36.1500]} {0x0 char } {[36.6150]} {[ 36.5980]}
C(cellfun(@(x)~isscalar(x) || ismissing(x),C)) = {-999};
T = cell2table(C);
C(rows,:)
ans = 20x2 cell array
{[33.8186]} {[ -999]} {[33.8898]} {[33.8903]} {[33.9370]} {[33.9366]} {[33.9753]} {[33.9761]} {[33.9749]} {[ -999]} {[33.9249]} {[33.9261]} {[33.9613]} {[33.9631]} {[34.1909]} {[34.1920]} {[34.2087]} {[ -999]} {[34.2238]} {[ -999]} {[36.3150]} {[36.3090]} {[36.3190]} {[36.3130]} {[36.3410]} {[36.3340]} {[36.2880]} {[36.2920]} {[36.1500]} {[ -999]} {[36.6150]} {[36.5980]}
T(rows,:)
ans = 20x2 table
C1 C2 ______ ______ 33.819 -999 33.89 33.89 33.937 33.937 33.975 33.976 33.975 -999 33.925 33.926 33.961 33.963 34.191 34.192 34.209 -999 34.224 -999 36.315 36.309 36.319 36.313 36.341 36.334 36.288 36.292 36.15 -999 36.615 36.598
Leon
Leon on 10 Mar 2025
Working now. Thank you so much for the tremendous help!

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!