How to assign a double value to a cell array?

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);

 Akzeptierte Antwort

Voss
Voss am 10 Mär. 2025
Bearbeitet: Voss am 10 Mär. 2025
Put curly braces {} around the -999, which makes the right-hand side of the assignment a scalar cell array:
T1(missingIndices2) = {-999};

8 Kommentare

Leon
Leon am 10 Mär. 2025
Thanks for the suggestion. I just tried it. Unfortunately, my table column turns into something like the below:
{[ -999]}
{[ -999]}
{[ -999]}
{[35.9963]}
{[35.8279]}
When it should be
-999
-999
-999
35.9963
35.8279
Voss
Voss am 10 Mär. 2025
Please upload a mat file with the cell array T1.
"Unfortunately, my table column turns into something like the below: "
It works here:
C = {-999; -999; -999; 35.9963; 35.8279};
T = cell2table(C)
T = 5x1 table
C ______ -999 -999 -999 35.996 35.828
Leon
Leon am 10 Mär. 2025
Please see attacned for a mat file containing the cell array T1. Many thanks for your help.
Stephen23
Stephen23 am 10 Mär. 2025
Bearbeitet: Stephen23 am 10 Mär. 2025
C = load('test.mat').T1
C = 1498x2 cell array
{[35.7030]} {[35.7010]} {[35.5790]} {[35.5780]} {[35.5790]} {[35.5790]} {[34.7100]} {[34.7090]} {[34.7180]} {[34.7190]} {[34.7170]} {[34.7130]} {[34.9420]} {[34.9410]} {[34.3970]} {[34.3820]} {[34.3970]} {[34.3820]} {[34.2390]} {[34.2390]} {[34.2390]} {[34.2390]} {[34.0750]} {[34.0710]} {[34.0760]} {[34.0670]} {[33.4280]} {[33.4240]} {[33.4280]} {[33.4230]} {[36.0710]} {[36.0740]}
C(~cellfun(@isscalar,C)) = {-999};
T = cell2table(C)
T = 1498x2 table
C1 C2 ______ ______ 35.703 35.701 35.579 35.578 35.579 35.579 34.71 34.709 34.718 34.719 34.717 34.713 34.942 34.941 34.397 34.382 34.397 34.382 34.239 34.239 34.239 34.239 34.075 34.071 34.076 34.067 33.428 33.424 33.428 33.423 36.071 36.074
This is probably better handled when the data is imported.
Leon
Leon am 10 Mär. 2025
Bearbeitet: Leon am 10 Mär. 2025
Many thanks.
There is one thing though. The code uses -999, why are the output NaNs instead? Additionally, the T1 I shared was a simplified one. In reality I would have many columns that are text strings as well. Is there a way I could replace missing values in float data columns with -999, but replace missing values in string columns with an empty string like ' '? Thanks.
C(~cellfun(@isscalar,C)) = {-999};
Voss
Voss am 10 Mär. 2025
Bearbeitet: Voss am 10 Mär. 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 am 10 Mär. 2025
Working now. Thank you so much for the tremendous help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2024b

Gefragt:

am 10 Mär. 2025

Kommentiert:

am 10 Mär. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by