ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
istype.m
Go to the documentation of this file.
1%> \brief
2%> Return ``true`` if and only if the input ``varval`` conforms with the
3%> specified input type ``vartype`` and the specified maximum size ``varsize``.
4%> Otherwise, return ``false``.
5%>
6%> \details
7%> Beware that this algorithm converts input values of type ``char`` to ``string``
8%> before further processing and type checking, that is, the types ``char`` to ``string``
9%> are assumed to be conformable and compatible with each other, like most other sane languages.<br>
10%>
11%> \param[in] varval : The input value to be converted to namelist-compatible value.
12%> \param[in] vartype : The input scalar MATLAB string containing the
13%> expected type of the value given by the input ``varval``.<br>
14%> The following type-conformance rules apply:<br>
15%> <ol>
16%> <li> if ``vartype`` is ``"string"``, then ``varval`` can be
17%> either a MATLAB ``string`` or ``char``.<br>
18%> An input value of type ``char`` is always
19%> converted to ``string`` before further processing.<br>
20%> <li> if ``vartype`` is ``"integer"``, then ``varval`` can be
21%> either a MATLAB ``int8``, ``int16``, ``int32``, ``int64``,
22%> or a whole-number ``real`` value.<br>
23%> <li> if ``vartype`` is ``"logical"``, then ``varval`` can be
24%> either a MATLAB ``int8``, ``int16``, ``int32``, ``int64``,
25%> a MATLAB ``real``, or a MATLAB ``logical`` value.<br>
26%> <li> if ``vartype`` is ``"complex"``, then ``varval`` can be
27%> either a MATLAB ``int8``, ``int16``, ``int32``, ``int64``,
28%> a MATLAB ``real``, or a MATLAB ``complex`` value.<br>
29%> <li> if ``vartype`` is ``"real"``, then ``varval`` can be
30%> either a MATLAB ``int8``, ``int16``, ``int32``, ``int64``,
31%> or a MATLAB ``real`` value (e.g., ``float``, ``single``, ``double``).<br>
32%> For all other object types, the type-conformance is verified by
33%> passing the input ``varval`` and ``vartype`` directly to the
34%> MATLAB intrinsic function ``isa()``.<br>
35%> </ol>
36%> \param[in] varsize : The input scalar MATLAB integer representing the
37%> maximum allowed size of the input value ``varval``.<br>
38%> (**optional**. If missing, the maximum length of the
39%> input ``varval`` will not be checked.)
40%>
41%> \return
42%> ``itis`` : The output scalar MATLAB logical that is ``true`` if and only if
43%> the input ``varval`` conforms with the specified input type ``vartype``
44%> and the specified maximum size ``varsize``, otherwise, it is ``false``.<br>
45%>
46%> \interface{istype}
47%> \code{.m}
48%>
49%> itis = pm.introspection.istype(varval, vartype)
50%> itis = pm.introspection.istype(varval, vartype, varsize)
51%>
52%> \endcode
53%>
54%> \example{istype}
55%> \include{lineno} example/introspection/istype/main.m
56%> \output{istype}
57%> \include{lineno} example/introspection/istype/main.out.m
58%>
59%> \final{istype}
60%>
61%> \author
62%> \JoshuaOsborne, May 21 2024, 5:47 PM, University of Texas at Arlington<br>
63%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
64%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
65function itis = istype(varval, vartype, varsize)
66 if ischar(varval)
67 varval = string(varval);
68 end
69 varvalen = numel(varval);
70 itis = false;
71 if 2 < nargin
72 itis = varvalen <= varsize;
73 if ~itis
74 return;
75 end
76 end
77 for i = 1 : varvalen
78 if isa(varval(i), "cell")
79 value = varval{i};
80 else
81 value = varval(i);
82 end
83 if strcmpi(vartype, "string") || strcmpi(vartype, "char")
84 itis = isa(value, "string") || isa(value, "char");
85 elseif strcmpi(vartype, "integer")
86 itis = isa(value, "int8") || isa(value, "int16") || isa(value, "int32") || isa(value, "int64");
87 if ~itis && isreal(value)
88 itis = rem(value, 1) == 0;
89 end
90 elseif strcmpi(vartype, "logical")
91 itis = isa(value, "logical") || isreal(value) || isa(value, "int8") || isa(value, "int16") || isa(value, "int32") || isa(value, "int64");
92 elseif strcmpi(vartype, "complex")
93 itis = isnumeric(value);
94 elseif strcmpi(vartype, "real") || strcmpi(vartype, "float") || strcmpi(vartype, "single") || strcmpi(vartype, "double")
95 itis = isreal(value) || isa(value, "int8") || isa(value, "int16") || isa(value, "int32") || isa(value, "int64");
96 else
97 itis = isa(value, vartype);
98 end
99 end
100end
function isnumeric(in str)
Return a scalar MATLAB logical that is true if and only if the input string can be converted to a sca...
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...