ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
len.m
Go to the documentation of this file.
1%> \brief
2%> Return a scalar MATLAB whole-number containing
3%> the length of the input scalar or vector object.
4%>
5%> \details
6%> The following rules apply to computing the length:
7%> <ol>
8%> <li> If the input ``obj`` is a scalar object of type ``char``
9%> or ``string`` and empty, then the output ``val`` is ``0``.
10%>
11%> <li> If the input ``obj`` is a scalar object of type ``char``
12%> or ``string`` and non-empty, then the output ``val`` is ``1``.
13%>
14%> <li> If the input ``obj`` is a scalar object of type other
15%> than ``char`` or ``string``, then the output ``val`` is ``1``.
16%> This is similar to the behavior of the MATLAB intrinsic ``length()``.
17%>
18%> <li> If the input ``obj`` is a vector and empty, then the output ``val`` is ``0``.
19%> This is similar to the behavior of the MATLAB intrinsic ``length()``.
20%>
21%> <li> If the input ``obj`` is a vector and non-empty,
22%> then the output ``val`` is the vector length as returned
23%> by ``length(obj)`` minus the number of empty elements.
24%> </ol>
25%>
26%> \param[in] obj : The input scalar or vector object whose length
27%> is to returned according to the rules above.
28%>
29%> \return
30%> `val` : The output scalar MATLAB whole-number
31%> representing the length of the input object.
32%>
33%> \interface{len}
34%> \code{.m}
35%>
36%> val = pm.array.len(obj);
37%>
38%> \endcode
39%>
40%> \example{len}
41%> \include{lineno} example/array/len/main.m
42%> \output{len}
43%> \include{lineno} example/array/len/main.out.m
44%>
45%> \final{len}
46%>
47%> \author
48%> \JoshuaOsborne, May 21 2024, 4:25 PM, University of Texas at Arlington<br>
49%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
50%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
51function val = len(obj)
52 if isa(obj, "char")
53 obj = string(obj);
54 end
55 % First ensure the input ``obj`` is a vector.
56 if numel(obj) ~= length(obj)
57 error ( newline ...
58 + "The input object must be a vector." + newline ...
59 + newline ...
60 );
61 end
62 val = 0;
63 for i = 1 : length(obj)
64 try
65 if ~strcmp(string(obj(i)), "")
66 val = val + 1;
67 end
68 catch
69 val = val + 1;
70 end
71 end
72end
function len(in obj)
Return a scalar MATLAB whole-number containing the length of the input scalar or vector object.