Multiple simultaneous assignments from another struct with set #14

Open
opened 2025-10-11 10:46:40 +00:00 by UserCurt · 1 comment
UserCurt commented 2025-10-11 10:46:40 +00:00 (Migrated from codeberg.org)

One use case that may often be employed is to copy data from one struct to another, possibly with different hierarchical structures. This feature request is to ease this process by integrating mpath.get as a part of mpath.set, piggybacking off of multiple simultaneous different assignments described in #13. The set function would have an additional input. Below is an example. An approach using solely the method from #13 is included for comparison. This example assigns every field, but this does not have to be the case generally.

S = struct(A=struct(X=1, Y=2), B=struct(X=3, Y=4))
T = struct(A=struct(X=5, Y=6), B=struct(X=7, Y=8))
R = mpath.set(S, "/*#1/*#2", num2cell(mpath.get(T, "/*#2/*#1")), MultiSet=true) % original approach
R = mpath.set(S, "/*#1/*#2", T, "/*#2/*#1") % proposed approach
% R would be: struct(A=struct(X=5, Y=7), B=struct(X=6, Y=8))
One use case that may often be employed is to copy data from one struct to another, possibly with different hierarchical structures. This feature request is to ease this process by integrating `mpath.get` as a part of `mpath.set`, piggybacking off of multiple simultaneous different assignments described in #13. The set function would have an additional input. Below is an example. An approach using solely the method from #13 is included for comparison. This example assigns every field, but this does not have to be the case generally. ```matlab S = struct(A=struct(X=1, Y=2), B=struct(X=3, Y=4)) T = struct(A=struct(X=5, Y=6), B=struct(X=7, Y=8)) R = mpath.set(S, "/*#1/*#2", num2cell(mpath.get(T, "/*#2/*#1")), MultiSet=true) % original approach R = mpath.set(S, "/*#1/*#2", T, "/*#2/*#1") % proposed approach % R would be: struct(A=struct(X=5, Y=7), B=struct(X=6, Y=8)) ```
Owner

The example has become a bit simpler, no longer needing num2cell while being more robust using UniformValues=false:

values = mpath.get(T, "/*#2/*#1", UniformPaths=true, UniformValues=false);
R = mpath.set(S, "/*#1/*#2", values, UniformPaths=true, UniformValues=false);
The example has become a bit simpler, no longer needing `num2cell` while being more robust using `UniformValues=false`: ```matlab values = mpath.get(T, "/*#2/*#1", UniformPaths=true, UniformValues=false); R = mpath.set(S, "/*#1/*#2", values, UniformPaths=true, UniformValues=false); ```
UserCurt modified the milestone from Release v1 to Someday 2026-07-27 12:27:25 +00:00
Sign in to join this conversation.
No description provided.