Commit f3be9c05 authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

misc.fieldnames: supports nested structs.

parent 1425596b
function fieldNames = fieldnames(myStruct)
% misc.fieldnames returns all field names of myStruct. If myStruct is a nested struct, i.e. a struct
% containing structs as fields, fields will return a string array containing all branches separating
% every layer with a dot, for instance (level1).(level2).(level3)...
%%
% Example
% misc.fieldnames(misc.setfield(struct("blub", 1337), "topLevel.lowerLevel.data", 42));
arguments
myStruct struct;
end
fieldNames = string([]);
for thisFieldName = string(fieldnames(myStruct)).'
fieldNames = [fieldNames; fieldNamesOfBranch(myStruct, thisFieldName)];
end % for thisFieldName
end % fieldnames()
function result = fieldNamesOfBranch(myStruct, branchName)
arguments
myStruct struct;
branchName (1, 1) string;
end % arguments
tmpField = misc.getfield(myStruct, branchName);
if isstruct(tmpField)
subBranchNames = branchName + "." + string(fieldnames(tmpField));
result = string([]);
for it = 1 : numel(subBranchNames)
result = [result; fieldNamesOfBranch(myStruct, subBranchNames(it))];
end
else
result = branchName;
end
end %
\ No newline at end of file
...@@ -2,15 +2,15 @@ function myValue = getfield(myStruct, myFieldName) ...@@ -2,15 +2,15 @@ function myValue = getfield(myStruct, myFieldName)
% misc.getfield returns data of a struct specified by its field name. % misc.getfield returns data of a struct specified by its field name.
% In contrast to the matlab built-in function getfield(), this method misc.getfield % In contrast to the matlab built-in function getfield(), this method misc.getfield
% can also read fields of field by specifying myFieldName with dots as seperator, for % can also read fields of field by specifying myFieldName with dots as seperator, for
% instance myFieldName = 'topLevel.lowerLevel.data', see example. % instance myFieldName = "topLevel.lowerLevel.data", see example.
% Additionally, misc.getfield can handle arrays of data, i.e. myFieldName string % Additionally, misc.getfield can handle arrays of data, i.e. myFieldName string
% arrays, by concatenating the result. % arrays, by concatenating the result.
%% %%
% Example 1: % Example 1:
% myStruct = misc.setfield(struct(), 'topLevel.lowerLevel.data', 42); % myStruct = misc.setfield(struct(), "topLevel.lowerLevel.data", 42);
% result = misc.getfield(myStruct, 'topLevel.lowerLevel.data') % result = misc.getfield(myStruct, "topLevel.lowerLevel.data")
% Example 2: % Example 2:
% myStruct = struct('a', 1, 'b', 2); % myStruct = struct("a", 1, "b", 2);
% result = misc.getfield(myStruct, ["a", "b"]) % result = misc.getfield(myStruct, ["a", "b"])
arguments arguments
...@@ -34,4 +34,5 @@ else % array case ...@@ -34,4 +34,5 @@ else % array case
for it = 1 : numel(myFieldName) for it = 1 : numel(myFieldName)
myValue = [myValue, misc.getfield(myStruct, myFieldName(it))]; myValue = [myValue, misc.getfield(myStruct, myFieldName(it))];
end end
end % plot() end % if isscalar(myFieldName)
\ No newline at end of file end % getfield()
\ No newline at end of file
...@@ -4,6 +4,19 @@ function [tests] = testMisc() ...@@ -4,6 +4,19 @@ function [tests] = testMisc()
tests = functiontests(localfunctions()); tests = functiontests(localfunctions());
end end
function testFieldnames(tc)
fieldNames = ["asdf", "blub.asdf.moeb", "bli.bla.blub", "tet.ris", "tetris", "ASDF", ...
"simData.plant.control", "simData.plant.controlOutput", "simData.flatOutput", ...
"blub.apfelsaft", "bli.blub.bla", "sonnen.schein", "sonne", "blub.asdf.blub.asdf"];
myStruct = struct();
for thisFieldName = fieldNames
myStruct = misc.setfield(myStruct, thisFieldName, rand(1));
end
fieldNamesFound = misc.fieldnames(myStruct);
tc.verifyEmpty(setdiff(fieldNames, fieldNamesFound));
end % testFieldnames()
function testPlace(tc) function testPlace(tc)
A = magic(2); A = magic(2);
B = ones(2, 1); B = ones(2, 1);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment