Assuming that the names are stored in a JavaScript array, one way to list all male names using MATLAB is to use regular expressions to match male names in the array. Here is an example of how to do this:
main.m401 chars12 lines
In this example, the regular expression (John|Robert|Michael|David)
matches the names "John", "Robert", "Michael", and "David". The regexp
function is used to apply this regular expression to each name in the array, and the cellfun
function is used to apply the isempty
function to each element of the resulting cell array to create a logical array indicating which elements of the original array matched the regular expression. The ~
operator is used to reverse this logical array, so that we end up with a logical array indicating which elements did not match the regular expression. Finally, we use this logical array to index into the original array to extract the male names. The strjoin
function is then used to create a comma-separated string of the male names, which is printed out using the fprintf
function.
gistlibby LogSnag