ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
copy.m
Go to the documentation of this file.
1%> \brief
2%> Copy the contents of the struct/object ``from``
3%> to the struct/object ``to`` recursively and without
4%> destroying the existing components in ``to``.<br>
5%>
6%> \param[in] from : The input scalar MATLAB struct whose (select)
7%> components must be copy/merged with the components
8%> of the input struct ``to``.<br>
9%> \param[in] to : The input scalar MATLAB struct to which the
10%> components of ``from`` struct must be copied.<br>
11%> (**optional**, default = ``struct()``)
12%> \param[in] fields : The input vector of MATLAB strings each element
13%> of which is a field name in ``from`` whose value
14%> has to be copied to the struct ``to``.<br>
15%> (**optional**, default = ``fieldnames(from)`` or ``properties(from)``)
16%>
17%> \return
18%> ``tonew`` : The output MATLAB struct containing the
19%> merger of the two input MATLAB structs.<br>
20%> If a field name in ``fields`` is common between
21%> ``from`` and ``to``, the field value of ``from`` will
22%> overwrite the corresponding field value of ``to``
23%> in the output ``tonew``.<br>
24%>
25%> \interface{copy}
26%> \code{.m}
27%>
28%> tonew = pm.matlab.struct.copy(from)
29%> tonew = pm.matlab.struct.copy(from, to)
30%> tonew = pm.matlab.struct.copy(from, [], fields)
31%> tonew = pm.matlab.struct.copy(from, to, fields)
32%>
33%> \endcode
34%>
35%> \example{copy}
36%> \include{lineno} example/matlab/struct/copy/main.m
37%> \output{copy}
38%> \include{lineno} example/matlab/struct/copy/main.out.m
39%>
40%> \final{copy}
41%>
42%> \author
43%> \JoshuaOsborne, May 21 2024, 10:59 PM, University of Texas at Arlington<br>
44%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
45%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
46function tonew = copy(from, to, fields)
47 if nargin < 3
48 try
49 fields = fieldnames(from);
50 catch
51 try
52 fields = properties(from);
53 catch
54 fields = [];
55 end
56 end
57 end
58 if nargin < 2
59 to = [];
60 end
61 if isempty(to)
62 to = struct();
63 end
64 if isempty(fields)
65 tonew = from;
66 return;
67 else
68 tonew = to;
69 end
70
71 for i = 1 : length(fields)
72 % check if the ``field`` is itself a struct or object.
73 try
74 subFieldList = fieldnames(from.(fields{i}));
75 catch
76 try
77 subFieldList = properties(from.(fields{i}));
78 catch
79 subFieldList = [];
80 end
81 end
82 if isempty(subFieldList)
83 tonewComponent = from.(fields{i});
84 else
85 try
86 tonewComponent = tonew.(fields{i});
87 catch % the ``field`` of from does not exist in to.
88 tonewComponent = struct();
89 end
90 tonewComponent = pm.matlab.struct.copy(from.(fields{i}), tonewComponent, subFieldList);
91 end
92 try % struct
93 tonew.(fields{i}) = tonewComponent;
94 catch % object
95 tonew.addprop(fields{i});
96 tonew.(fields{i}) = tonewComponent;
97 end
98 end
99end
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared libraries.
function copy(in from, in to, in fields)
Copy the contents of the struct/object from to the struct/object to recursively and without destroyin...
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...