Check if the value for a field in a struct exists

11 visualizaciones (últimos 30 días)
black cat
black cat el 10 de Feb. de 2025
Comentada: black cat el 10 de Feb. de 2025
A struct can have a field name with no value. For example:
A=struct('a',{})
How can I check if this is the case?
I tried
isempty(A.a)
But an error says not enough input arguments.

Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Feb. de 2025
A=struct('a', {})
A = 0x0 empty struct array with fields: a
isempty(A)
ans = logical
1
isempty(A) || isempty(A.a)
ans = logical
1

Más respuestas (1)

Matt J
Matt J el 10 de Feb. de 2025
Editada: Matt J el 10 de Feb. de 2025
Watch your struct() syntax,
A=struct('a',[]) %field 'a' with empty matrix
A = struct with fields:
a: []
isempty(A.a)
ans = logical
1
B=struct('a',{{}}) %field 'a' with empty cell array
B = struct with fields:
a: {}
isempty(B.a)
ans = logical
1

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by