existField

버전 1.0.0.3 (2.4 KB) 작성자: Mitchell Tillman
Determine if a field exists at a specific location within a struct.
다운로드 수: 2
업데이트 날짜: 2022/3/4

라이선스 보기

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')

인용 양식

Mitchell Tillman (2024). existField (https://www.mathworks.com/matlabcentral/fileexchange/107509-existfield), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2021b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.0.0.3

Tweaked description

1.0.0.2

Tweaked description

1.0.0.1

Tweaked description

1.0.0