How to assign a double value to a cell array?

조회 수: 3 (최근 30일)
Leon
Leon 2025년 3월 10일
댓글: Leon 2025년 3월 10일
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);

채택된 답변

Voss
Voss 2025년 3월 10일
편집: Voss 2025년 3월 10일
Put curly braces {} around the -999, which makes the right-hand side of the assignment a scalar cell array:
T1(missingIndices2) = {-999};
  댓글 수: 8
Voss
Voss 2025년 3월 10일
편집: Voss 2025년 3월 10일
"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 2025년 3월 10일
Working now. Thank you so much for the tremendous help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by