How to detect missing values in a string cell array

조회 수: 15 (최근 30일)
Leon
Leon 2025년 3월 8일
댓글: Walter Roberson 2025년 3월 9일
I have a cell array A like the below:
Columns 1 through 4
{'NAME'} {'STNNBR'} {[<missing>]} {'SAMPNO'} .
I tried to find the location of the missing value by doing the below:
B = ismissing(A)
Why is the value of B zero for all of them? Shouldn't the answer be: 0 0 1 0?
Columns 1 through 4
0 0 0 0
What is the correct way to write this code? Thanks.
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 3월 9일

Ah, but the missing element is not a character vector, so this is not a cell array of character vectors

Stephen23
Stephen23 2025년 3월 9일
"Why is the value of B zero for all of them? Shouldn't the answer be: 0 0 1 0?"
The ISMISSING documentation states that for a cell array of character vector ISMISSING returns TRUE for empty character vectors: "Missing values are defined according to the data type of A ... {''}cell of character vectors"
Your data do NOT match this requirement.
"What is the correct way to write this code?"
Use a STRING array:
A = ["NAME", "STNNBR", missing, "SAMPNO"]
ismissing(A)

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

채택된 답변

Walter Roberson
Walter Roberson 2025년 3월 8일
You are applying ismissing() to a cell array. That is the same thing as calling
[ismissing(A(1)), ismissing(A(2)), ismissing(A(3)), ismissing(A(4))]
A(1), A(2), A(3), A(4) are all cells -- and the cells are not missing.
You should probably be using a string array rather than a cell array,
A = ["NAME", "STNNBR", nan, "SAMPNO"]
A = 1x4 string array
"NAME" "STNNBR" <missing> "SAMPNO"
ismissing(A)
ans = 1x4 logical array
0 0 1 0
but if you insist on using a cell array then you should have something more like
B = {'NAME', 'STNNBR', string(nan), 'SAMPNO'};
cellfun(@(X)any(ismissing(X),'all'), B)
ans = 1x4 logical array
0 0 1 0
  댓글 수: 3
Image Analyst
Image Analyst 2025년 3월 9일
I always get confused as to when I should use ismissing or isempty. It seems no matter which one I use first, it's always the second one that I should use. Any intuitive rule of thumb as to use the right one?
Walter Roberson
Walter Roberson 2025년 3월 9일
isempty: There is no data here.
ismissing(): There is data here, but some of it is marked as being invalid.
if isempty([]); disp('empty'); else; disp('not empty'); end
empty
if ismissing([]); disp('missing'); else; disp('not missing'); end
not missing
C = {'ab', [1 2 missing], [], missing, ["cd", missing, "ef"]}
C = 1x5 cell array
{'ab'} {[1 2 NaN]} {0x0 double} {[<missing>]} {["cd" <missing> "ef"]}
cellfun(@isempty, C, 'uniform', 0)
ans = 1x5 cell array
{[0]} {[0]} {[1]} {[0]} {[0]}
cellfun(@ismissing, C, 'uniform', 0)
ans = 1x5 cell array
{[0 0]} {[0 0 1]} {0x0 logical} {[1]} {[0 1 0]}

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by