ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
locname.m
Go to the documentation of this file.
1%> \brief
2%> Return the lists of values and indices of elements
3%> of the input ``strlist`` that match the input scalar
4%> (or vector of) string(s) or integer(s) index ``keylist``.<br>
5%>
6%> \details
7%> Beware that all specified values in the input ``keylist``
8%> must exist in the input argument ``strlist``, otherwise,
9%> the procedure will interrupt the program by raising an exception.
10%>
11%> \param[in] strlist : The input scalar (or vector of) MATLAB string(s).<br>
12%> \param[in] keylist : The input scalar (or vector of) MATLAB string(s)
13%> or cell array of char vectors or vector of MATLAB
14%> integers or a cell array of mix of the element
15%> types to match the input ``strlist``.<br>
16%>
17%> \return
18%> ``loclist`` : The output scalar (or vector of same size as ``keylist`` of)
19%> MATLAB integer(s) containing the location(s) of the occurrence(s)
20%> of the input ``keylist``.<br>
21%> <br>
22%> ``namlist`` : The output scalar (or vector of same size as ``keylist`` of)
23%> MATLAB string(s) containing the name(s) of the occurrence(s)
24%> of the input ``keylist``.<br>
25%>
26%> \interface{locname}
27%> \code{.m}
28%>
29%> [loclist, namlist] = pm.str.locname(strlist, keylist)
30%>
31%> \endcode
32%>
33%> \example{locname}
34%> \include{lineno} example/str/locname/main.m
35%> \output{locname}
36%> \include{lineno} example/str/locname/main.out.m
37%>
38%> \final{locname}
39%>
40%> \author
41%> \JoshuaOsborne, May 21 2024, 4:38 AM, University of Texas at Arlington<br>
42%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
43%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
44function [loclist, namlist] = locname(strlist, keylist)
45
46 keylistLen = pm.array.len(keylist);
47 strlist = string(strlist);
48 if keylistLen == 0
49 loclist = 1 : 1 : length(strlist);
50 namlist = strlist;
51 return;
52 end
53
54 failed = false;
55 strlistLen = length(strlist);
56 for i = keylistLen : -1 : 1
57 failed = true;
58 key = keylist(i);
59 if isa(key, "cell")
60 key = key{1};
61 end
62 if isa(key, "string") || isa(key, "char")
63 %namlist(i) = key;
64 for j = 1 : strlistLen
65 if strcmpi(key, strlist(j))
66 namlist(i) = strlist(j);
67 loclist(i) = j;
68 failed = false;
69 break;
70 end
71 end
72 if failed
73 % This situation happens because of automatic conversion of numbers
74 % to strings in MATLAB array constructs like a = ["paramonte", 1];
75 key = str2double(key);
76 failed = isnan(key);
77 if ~failed
78 failed = key < 1 || strlistLen < key;
79 if ~failed
80 loclist(i) = key;
81 namlist(i) = strlist(key);
82 end
83 end
84 end
85 elseif isnumeric(key)
86 loclist(i) = key;
87 failed = strlistLen < loclist(i);
88 if ~failed
89 namlist(i) = string(strlist{loclist(i)});
90 end
91 end
92 if failed
93 break;
94 end
95 end
96
97 if failed
98 help("pm.str.locname");
99 disp("strlist = ");
100 disp(strlist);
101 disp("keylist = ");
102 disp(keylist);
103 error ( newline ...
104 + "The element #" + string(i) + " of the input ``keylist`` above" + newline ...
105 + "argument does not match any elements of the input ``strlist`` above." + newline ...
106 + newline ...
107 );
108 end
109
110end
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function index(in array)
Given array(1 : n), return the array index indx(1 : n) such that array(indx) is in ascending order.
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 locname(in strlist, in keylist)
Return the lists of values and indices of elements of the input strlist that match the input scalar (...