ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
getEntryNML.m
Go to the documentation of this file.
1%> \brief
2%> Return a Fortran-namelist-compatible conversion of the input ``varval``.<br>
3%>
4%> \details
5%> This functionality is primarily used by the ParaMonte MATLAB internal
6%> routines to communicate information with Fortran shared libraries.<br>
7%> As such, it is of limited to most end users of the library.<br>
8%>
9%> \param[in] varname : The input scalar MATLAB string containing the label to assign
10%> to the namelist-converted value in the output ``entry``.<br>
11%> The specified value of ``varname`` will be trimmed
12%> (to remove leading and trailing blanks).<br>
13%> \param[in] varval : The input value to be converted to namelist-compatible value.
14%> \param[in] vartype : See the documentation of the corresponding
15%> argument of [pm.introspection.istype()](@ref istype).
16%> \param[in] varsize : See the documentation of the corresponding
17%> argument of [pm.introspection.istype()](@ref istype).
18%>
19%> \return
20%> ``entry`` : The output scalar MATLAB string containing the namelist-compatible
21%> conversion of the input value ``varval`` and the given ``varname``
22%> in the following format: ``varname=namelist-compatible-varval``.
23%>
24%> \interface{getEntryNML}
25%> \code{.m}
26%>
27%> entry = pm.introspection.getEntryNML(varname, varval, vartype, varsize)
28%>
29%> \endcode
30%>
31%> \note
32%> If the input value is string, it will be quoted properly.<br>
33%> If the input ``varval`` is an array, its elements will be comma-separated.<br>
34%>
35%> \example{getEntryNML}
36%> \include{lineno} example/introspection/getEntryNML/main.m
37%> \output{getEntryNML}
38%> \include{lineno} example/introspection/getEntryNML/main.out.m
39%>
40%> \final{getEntryNML}
41%>
42%> \author
43%> \JoshuaOsborne, May 21 2024, 5:38 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 entry = getEntryNML(varname, varval, vartype, varsize)
47 varval = varval(:);
48 varvalen = numel(varval);
49 %varname = string(inputname(2));
50 varname = string(strtrim(varname));
51 if pm.introspection.istype(varval, vartype, varsize)
52 entry = varname + "=";
53 delim = " ";
54 vartype = lower(vartype);
55 for i = 1 : varvalen
56 if isa(varval(i), "cell")
57 value = varval{i};
58 else
59 value = varval(i);
60 end
61 if strcmp(vartype, "string")
62 entry = entry + pm.str.quote(string(value)) + delim;
63 elseif strcmp(vartype, "integer")
64 if rem(value, 1) == 0
65 entry = entry + value + delim;
66 else
67 entry = entry + round(value) + delim;
68 end
69 elseif strcmp(vartype, "logical")
70 if value
71 entry = entry + ".true." + delim;
72 else
73 entry = entry + ".false." + delim;
74 end
75 elseif strcmp(vartype, "complex")
76 entry = entry + "(" + string(real(value)) + "," + string(imag(value)) + ")" + delim;
77 elseif strcmp(vartype, "real") || strcmpi(vartype, "float") || strcmpi(vartype, "single") || strcmpi(vartype, "double")
78 entry = entry + value + delim;
79 else
80 help("pm.introspection.getEntryNML");
81 disp("vartype");
82 disp( vartype );
83 error ( newline ...
84 + "Unrecognized input ``vartype`` value." + newline ...
85 + newline ...
86 );
87 end
88 end
89 else
90 disp(varname + " = ");
91 disp(varval);
92 error ( newline ...
93 + "The input " + varname + " specification value(s) displayed" + newline ...
94 + "above must be conformable to a MATLAB " + vartype + " type," + newline ...
95 + "with a maximum " + string(varsize) + " number of elements." + newline ...
96 + "The specified value has the class:" + newline ...
97 + newline ...
98 + " class(" + varname + ") = " + string(class(varval)) + newline ...
99 + newline ...
100 + "with size:" + newline ...
101 + newline ...
102 + " size(" + varname + ") = [" + join(string(size(varval)), ", ") + "]" + newline ...
103 + newline ...
104 );
105 end
106end
function getEntryNML(in varname, in varval, in vartype, in varsize)
Return a Fortran-namelist-compatible conversion of the input varval.
function istype(in varval, in vartype, in varsize)
Return true if and only if the input varval conforms with the specified input type vartype and the sp...
function quote(in str)
Return the input scalar MATLAB string as doubly quoted string such that the first and last character ...