Commit 30b279fb authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

misc.structMerge: new function to merge structs

parent 609d63b1
function newStruct = structMerge(myStructs, NameValue)
% misc.structMerge merges all structs to one struct.
%
% newStruct = misc.StructMerge(struct1, struct2, ...) merges all fields of all structs struct1,
% struct2, ... into newStruct. An error is thrown, if structs contain same fields that would
% be overwritten.
%%
% Example
% misc.structMerge(struct("asdf", 42), struct("blub", 1337));
arguments (Repeating)
myStructs struct;
end
arguments
NameValue.noOverwrite (1, 1) logical = false;
end
if nargin == 0
newStruct = struct();
else
newStruct = myStructs{1};
for it = 2 : numel(myStructs)
tmpFieldnames = misc.fieldnames(myStructs{it});
for tmpFieldname = tmpFieldnames.'
if NameValue.noOverwrite
if contains(misc.fieldnames(newStruct), tmpFieldname)
error("misc:structMerge:overwriteField", ...
"setting " + tmpFieldname + " will overwrite an existing field")
end
end
newStruct = misc.setfield(...
newStruct, tmpFieldname, misc.getfield(myStructs{it}, tmpFieldname));
end % for tmpFieldname = tmpFieldnames.'
end % for it = 2 : numel(myStructs)
end % if nargin == 0
end % misc.structMerge()
\ No newline at end of file
......@@ -4,17 +4,39 @@ function [tests] = testMisc()
tests = functiontests(localfunctions());
end
function testStructMerge(tc)
newStruct = misc.structMerge(struct("asdf", 42), struct("blub", 1337));
tc.verifyEqual(misc.fieldnames(newStruct), ["asdf"; "blub"]);
tc.verifyEqual(newStruct.asdf, 42);
tc.verifyEqual(newStruct.blub, 1337);
%
newStruct = misc.structMerge(misc.setfield(struct(), "asdf.blub", 42), struct("asdf", 1337), ...
"noOverwrite", false);
tc.verifyEqual(misc.fieldnames(newStruct), "asdf");
tc.verifyEqual(newStruct.asdf, 1337);
%
fun = @() misc.structMerge(misc.setfield(struct(), "asdf.blub", 42), struct("asdf", 1337), ...
"noOverwrite", true);
tc.verifyError(fun, "misc:structMerge:overwriteField");
%
newStruct = misc.structMerge(misc.setfield(struct(), "asdf.blub", 42), ...
misc.setfield(struct(), "asdf.bli", 1337), "noOverwrite", true);
tc.verifyEqual(misc.fieldnames(newStruct), ["asdf.blub"; "asdf.bli"]);
tc.verifyEqual(newStruct.asdf.blub, 42);
tc.verifyEqual(newStruct.asdf.bli, 1337);
end % testStructMerge()
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));
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)
......
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