existField

version 1.0.0.3 (2.4 KB) by Mitchell Tillman
Determine if a field exists at a specific location within a struct.

2 Downloads

Updated 4 Mar 2022

View License

This function returns true if a field exists at a specific location within the struct. This is useful if you want to know if a field exists only within one position in the struct, especially for non-scalar structs. Use existField if you want to know about one specific location in a struct, e.g. if you want to know if a.b.c exists but don't care if a.c exists, and for non-scalar structs. This is in contrast to existing options such as hasfield and isfieldrecursive, which examine the struct as a whole with no specificity for one specific struct location, and also do not handle non-scalar fields.
existField Syntax:
boolean=existField(struct,path,varargin);
% Inputs:
% struct: The structure to look in (struct)
% path: The path name to the field to check for (char)
% varargin: Variables for dynamic path names (char) or dynamic indexing (numeric)
% NOTE: Must be in the same order that they appear in the struct path
% Outputs:
% boolean: true if the field exists at the specified location, false if not (boolean)
existField Examples:
% Create a sample structure
struct.a(1).b=1;
struct.b(1,2).c=1;
struct.d(2).e=1;
fldName='b'; % Dynamic field name
num=1; % Dynamic index
% Examples that return true.
existField(struct.a,'b'); % The same as the builtin isfield
existField(struct,'struct.a.b'); % Check a subfield within the struct by specifying its full location
existField(struct,'struct.d(2).e'); % Hard-coded vector indexing
existField(struct,'struct.d(num).e',num); % Dynamic vector indexing
existField(struct,'struct.b(num,2).c',num); % Mix of hard-coded & dynamic matrix indexing
existField(struct,'struct.(fldName)(1,2).c',fldName); % Dynamic field name with hard-coded indexing
existField(struct,'struct.(fldName)(num,2).c',fldName,num); % Dynamic field name and dynamic indexing
% Examples that return false.
existField(struct.b,'e'); % The same as the builtin isfield
existField(struct,'struct.b.e'); % Check a subfield within the struct by specifying its full location
existField(struct,'struct.d(3).e'); % Hard-coded vector indexing. size(struct.d,1)<3
existField(struct,'struct.d(num).e',3); % Dynamic vector indexing. size(struct.d,1)<3
existField(struct,'struct.b(num,2).c',3); % Mix of hard-coded & dynamic matrix indexing. size(struct.b,1)<3
existField(struct,'struct.(fldName)(3,2).c',fldName); % Dynamic field name with hard-coded indexing. size(struct.b,1)<3
existField(struct,'struct.(fldName)(3,num).c',fldName); % Dynamic field name and dynamic indexing. size(struct.b,1)<3
hasfield and isfieldrecursive do not handle non-scalar structs:
% Adapted from isfieldrecursive FileExchange description
myStructure.a(1).calibration1.left.fc = 1;
myStructure.a(2).calibration2.right.fc = 1;
myStructure.a(3).calibration3.centre.fc = 1;
% Returns false because it can't be indexed
isfieldRecursive(myStructure,'a','calibration3','centre')
% Returns true because index is specified
existField(myStructure,'myStructure.a(3).calibration3.centre')
% Adapted from hasfield FileExchange description
d.b.d.b(2).a.waldo = 'where?'
d.b.d.b(1).a.nowaldo='nowhere'
% Throws an error! Doesn't handle non-scalar structs
[x,L] = hasfield(d,'waldo')
% Returns true, as it should
existField(d,'d.c.d.b(2).a.waldo')
% Returns true and L=1 because it doesn't distinguish between fields of the same name at different levels
[x,L] = hasfield(d,'b')

Cite As

Mitchell Tillman (2022). existField (https://www.mathworks.com/matlabcentral/fileexchange/107509-existfield), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2021b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!