Skip to content
Snippets Groups Projects
Commit eef71ec1 authored by leprob001's avatar leprob001
Browse files

Added hasMember and getMember function.

With these functions it is possible to check a complicated struct if a
specific member exists.
parent 3afbf5e2
No related branches found
No related tags found
No related merge requests found
function [memberValue] = getMember(pStructure, pMember, pDefaultValue)
%GETMEMBER Returns the given member in the structure if existent, the given default value otherwise.
% Checks for the existence of the member by using artoa.data.hasMember.
% If the value exists, it will be returned. Otherwise it will return the
% default value.
%
% Parameters:
% pStructure (struct) The structure where the member is stored in.
% pMember (cell) A 1D cell of chars, each entry representing the
% level of the structure.
% pDefaultValue (mixed) The default value that is returned if the
% member does not exist.
%% Initialization
if ~exist(pDefaultValue, 'var')
pDefaultValue = '';
end
memberValue = pDefaultValue;
%% Return member if it exists
if ~artoa.data.hasMember(pStructure, pMember)
return;
end
memberValue = pStructure;
for i = 1:length(pMember)
memberValue = memberValue.(pMember{i});
end
end
function [boolean] = hasMember(pStructure, varargin)
%HASVARIABLE Checks the given structure if the member is available.
% Checks if the fields are available in the given structure.
% Example:
% hasMember(S, 'hello', 'world')
% checks if S.hello.world member is available.
% Parameters:
% pStructure (struct) The structure where the member will be
% searched for.
% varargin (char) Every additional char array represents the
% level of the structure.
%
% Returns (bool) False if the member is not available.
%% Initialization
boolean = false;
%% Parameter check
if isempty(varargin)
return;
end
if length(varargin) == 1 && iscell(varargin{1})
varargin = varargin{1};
end
%% Check all levels
currentLevel = pStructure;
for i = 1:length(varargin)
if isfield(currentLevel, varargin{i})
currentLevel = currentLevel.(varargin{i});
else
return;
end
end
boolean = true;
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment