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