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