Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LRT_infinite_dimensional_systems
conI
Commits
f3be9c05
Commit
f3be9c05
authored
Oct 26, 2020
by
Jakob Gabriel
Browse files
misc.fieldnames: supports nested structs.
parent
1425596b
Changes
3
Hide whitespace changes
Inline
Side-by-side
+misc/fieldnames.m
0 → 100644
View file @
f3be9c05
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
+misc/getfield.m
View file @
f3be9c05
...
...
@@ -2,15 +2,15 @@ function myValue = getfield(myStruct, myFieldName)
% 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
% 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
% arrays, by concatenating the result.
%%
% Example 1:
% myStruct = misc.setfield(struct(),
'
topLevel.lowerLevel.data
'
, 42);
% result = misc.getfield(myStruct,
'
topLevel.lowerLevel.data
'
)
% myStruct = misc.setfield(struct(),
"
topLevel.lowerLevel.data
"
, 42);
% result = misc.getfield(myStruct,
"
topLevel.lowerLevel.data
"
)
% Example 2:
% myStruct = struct(
'a'
, 1,
'b'
, 2);
% myStruct = struct(
"a"
, 1,
"b"
, 2);
% result = misc.getfield(myStruct, ["a", "b"])
arguments
...
...
@@ -34,4 +34,5 @@ else % array case
for
it
=
1
:
numel
(
myFieldName
)
myValue
=
[
myValue
,
misc
.
getfield
(
myStruct
,
myFieldName
(
it
))];
end
end
% plot()
\ No newline at end of file
end
% if isscalar(myFieldName)
end
% getfield()
\ No newline at end of file
+unittests/+misc/testMisc.m
View file @
f3be9c05
...
...
@@ -4,6 +4,19 @@ function [tests] = testMisc()
tests
=
functiontests
(
localfunctions
());
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
)
A
=
magic
(
2
);
B
=
ones
(
2
,
1
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment