Check if a field within a structure exist

61 visualizaciones (últimos 30 días)
Luis Eduardo Cofré Lizama
Luis Eduardo Cofré Lizama el 6 de Mzo. de 2023
Editada: Stephen23 el 6 de Mzo. de 2023
I have a structure with data for different periods (9) of a timeseries. Sometimes within a structure there's a field called "Breaks". e.g. Data.Period_4.Breaks, whicjh contains 2 duration values. I need to check for each period (1:9) if the field Breaks exist. However when doing the following below, it does always provide "false". Am I missing something or not doing properly? Also, can I use "~isfield"... it does not say no when used.
for i=1:9
chunk = strcat('Period_',num2str(i));
if isfield(strcat('Data.',chunk),Breaks)
data = xyz;
elseif ~isfield(strcat('Data.',chunk),Breaks)
data =zyx;
end
Regards
Eduardo

Respuestas (1)

Voss
Voss el 6 de Mzo. de 2023
for i=1:9
chunk = sprintf('Period_%d',i);
if isfield(Data.(chunk),'Breaks')
data = xyz;
else
data = zyx;
end
end

Categorías

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

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by