MATLAB Examples

strlookup

This function uses strcmp or strcmpi to return indices of a list of strings matching an input string. If no matches are found, close matches are suggested.

Contents

Syntax

ind = strlookup('string',list)
ind = strlookup(...,'CaseSensitive')
ind = strlookup(...,'threshold',ThresholdValue)
[ind,CloseNames] = strlookup(...)

Description

ind = strlookup('string',list) returns indices ind corresponding to cell entries in list matching 'string'.

ind = strlookup(...,'CaseSensitive') performs a case-sensitive strlookup.

ind = strlookup(...,'threshold',ThresholdValue) declares a threshold value for close matches. You will rarely (if ever) need to use this. The ThresholdValue is a metric of how closely matches should be when offering suggestions. Low threshold values limit suggested matches to a shorter list whereas high thresholds expand the list size. By default, the threshold starts at 1.5, then increases or decreases depending on how many close matches are returned. If fewer than 3 close matches are returned, the threshold is increased and it looks for more matches. If more than 10 close matches are found, the threshold is tightened (reduced) until fewer than 10 matches are found.

[ind,CloseNames] = strlookup(...) suppresses command window output if no exact match is found, and instead returns an empty matrix ind and a cell array of close matches in names. If exact match(es) is/are found, ind will be populated and CloseNames will be empty.

Examples

You have some long list of strings and you're calling it mylist:

mylist = {'potatoes';'bananas';'bandanas';'pleated pants';...
    'more bananas';'conscientious';'yellow';'jello';'YOLO';...
    'solo';'my dog ate my homework';'your fish ate my dog';...
    'lasagna-flavored tape';'yell';'WATCH OUT FOR THOSE PLANTS!';...
    'Bicycles are nice to ride.';'spleen canteen';'misspell';...
    'Bananas';'occasionally';'rot';'Dippin'' Dots';'are still the future'};

Now you want indices of all the cells in mylist matching bananas.

ind = strlookup('bananas',mylist)
ind =
     2
    19

That's exactly what we'd expect. Cells 2 and 19 are filled with bananas. See:

mylist(ind)
ans = 
    'bananas'
    'Bananas'

What if you want to do a case-sensitive search?

strlookup('Bananas',mylist,'CaseSensitive')
ans =
    19

Let's face it, bananas is actually kind of hard to spell. I am sure you'd have no trouble spelling bananas, but whomever uses your banana-finder function might have a tough time with the spelling. When he or she inevitably misspells bananas, strlookup will offer some help:

strlookup('banannas',mylist)
String 'banannas' not found. Did you mean...
    'bananas'
    'bandanas'
    'Bicycles are nice to ride.'
    'Bananas'

The user may even accidentally switch the order of inputs to strlookup and it will still work:

strlookup(mylist,'yellow')
ans =
     7

A theshold for near matches may be set. For lots of near matches, set the threshold high; for fewer near matches, set the threshold low:

strlookup('hello',mylist)
String 'hello' not found. Did you mean...
    'yellow'
    'jello'
    'yell'

That's not enough matches; let's raise the threshold:

strlookup('hello',mylist,'threshold',3)
String 'hello' not found. Did you mean...
    'yellow'
    'jello'
    'YOLO'
    'solo'
    'yell'
    'misspell'
    'rot'

That's too many matches. Let's set the threshold very low:

strlookup('hello',mylist,'threshold',1.5)
String 'hello' not found. Did you mean...
    'jello'

An example of an application of strlookup is seen in the rgb function, which loads a list of color names and corresponding RGB values:

load xkcd_rgb_data
whos
  Name             Size             Bytes  Class     Attributes

  ans              1x1                  8  double              
  colorlist      949x1             125800  cell                
  ind              2x1                 16  double              
  mylist          23x1               3128  cell                
  rgblist        949x3              22776  double              

If a user enters rgb('blue-green'), rgb will not find an exact match, but some similar matches do exist:

strlookup('blue-green',colorlist)
String 'blue-green' not found. Did you mean...
    'green/blue'
    'greenblue'
    'blue/green'
    'bluegreen'
    'green blue'
    'blue green'

Author Info

This function written by Chad A. Greene of the University of Texas at Austin's Institute for Geophysics (UTIG) in August of 2014. This function was built on a great suggestion by Cedric Wannaz on the Mathworks Answers forum. Thanks Cedric.