File Exchange

image thumbnail


version (11.3 KB) by Chad Greene
Find strings in a list of cells and return near matches if no exact match is found.

1 Download

Updated 11 Jan 2015

View Version History

View License

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.

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

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.

Cite As

Chad Greene (2021). strlookup (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Chad Greene

Good catch Bryan, and thank you for the idea of how to fix it. I've made the change you suggested. I appreciate your thoughtful feedback.


This is great! Very nice and useful -- the ability to return NearbyNames is great. Code is well commented and the documentation is very useful.

However, I did find one bug. You do some parsing of the user input, checking for whether the user accidentally swapped the required inputs 'string' and 'list' by using numel() ... in the [probably rare] case that 'string' has fewer characters than there are elements in 'list', this causes the two inputs to be incorrectly swapped. EX:

% this works because 'ap' is shorter than the list
strlookup('ap', {'orange', 'apple', 'peach'});

% this fails because 'appl' is "longer" than the list
% inputs are incorrectly swapped, leading to an error
strlookup('appl', {'orange', 'apple', 'peach'});

You can instead use this at line 49 (or remove this input parsing altogether):

if ischar(list) && iscell(string)

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!