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] field : The input vector of MATLAB strings each element
13%> of which is a field or property name in ``from``
14%> whose value has to be copied to the struct ``to``.<br>
15%> (**optional**, default = ``fieldnames(from)`` or ``properties(from)``)
16%> \param[in] exclude : The input vector of MATLAB strings each element
17%> of which is a field or property name in ``from``
18%> whose value has to be skipped and excluded from the copy to the struct ``to``.<br>
19%> (**optional**, default = ``[]``)
20%>
21%> \return
22%> ``tonew`` : The output MATLAB struct containing the
23%> merger of the two input MATLAB structs.<br>
24%> If a field name in ``field`` is common between
25%> ``from`` and ``to``, the field value of ``from`` will
26%> overwrite the corresponding field value of ``to``
27%> in the output ``tonew``.<br>
28%>
29%> \interface{copy}
30%> \code{.m}
31%>
32%> tonew = pm.matlab.copy(from)
33%> tonew = pm.matlab.copy(from, to)
34%> tonew = pm.matlab.copy(from, [], field)
35%> tonew = pm.matlab.copy(from, [], field, [])
36%> tonew = pm.matlab.copy(from, to, field, [])
37%> tonew = pm.matlab.copy(from, to, field, exclude)
38%> tonew = pm.matlab.copy(from, to, [], exclude)
39%> tonew = pm.matlab.copy(from, [], [], exclude)
40%>
41%> \endcode
42%>
43%> \warning
44%> If a name appears in both ``field`` and ``exclude`` input arguments, then it is excluded.<br>
45%>
46%> \example{copy}
47%> \include{lineno} example/matlab/copy/main.m
48%> \output{copy}
49%> \include{lineno} example/matlab/copy/main.out.m
50%>
51%> \final{copy}
52%>
53%> \author
54%> \JoshuaOsborne, May 21 2024, 10:59 PM, University of Texas at Arlington<br>
55%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
56%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
57function tonew = copy(from, to, field, exclude)
58
59 if nargin < 4
60 exclude = [];
61 elseif pm.array.len(exclude) == 0
62 exclude = [];
63 else
64 exclude = [string(exclude)];
65 end
66
67 if nargin < 3
68 field = [];
69 end
70 if isempty(field)
71 try
72 field = fieldnames(from);
73 catch
74 try
75 field = properties(from);
76 catch
77 field = [];
78 end
79 end
80 end
81
82 if nargin < 2
83 to = [];
84 end
85 if isempty(to)
86 to = struct();
87 end
88
89 if isempty(field)
90 tonew = from;
91 return;
92 else
93 tonew = to;
94 end
95
96 for i = 1 : length(field)
97
98 %%%%
99 %%%% Skip the ``field`` if it is excluded.
100 %%%%
101
102 if isempty(exclude) || ~any(strcmp(exclude, field(i)))
103
104 %%%%
105 %%%% Check if the ``field`` is itself a struct or object.
106 %%%%
107
108 try
109 subFieldList = fieldnames(from.(field{i}));
110 catch
111 try
112 subFieldList = properties(from.(field{i}));
113 catch
114 subFieldList = [];
115 end
116 end
117
118 %%%%
119 %%%% Create the new copy component.
120 %%%%
121
122 if isempty(subFieldList)
123 tonewComponent = from.(field{i});
124 else
125 try
126 tonewComponent = tonew.(field{i});
127 catch % the ``field`` of from does not exist in to.
128 tonewComponent = struct();
129 end
130 tonewComponent = pm.matlab.copy(from.(field{i}), tonewComponent, subFieldList);
131 end
132
133 %%%%
134 %%%% Copy the field values.
135 %%%%
136
137 try % struct
138 tonew.(field{i}) = tonewComponent;
139 catch % object
140 tonew.addprop(field{i});
141 tonew.(field{i}) = tonewComponent;
142 end
143
144 end
145
146 end
147
148end
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function copy(in from, in to, in field, in exclude)
Copy the contents of the struct/object from to the struct/object to recursively and without destroyin...
excluded
Definition: show.m:173
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...