View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Find nearest-matching string from a set

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.0 | 2 ratings Rate this file 11 Downloads (last 30 days) File Size: 2.43 KB File ID: #36981 Version: 1.2

Find nearest-matching string from a set



04 Jun 2012 (Updated )

Computes shortest distance between a key string and a set of strings to find the nearest match.

| Watch this File

File Information

Uses a modified Vagner-Fischer algorithm to find the Levenshtein distance between each pair of strings. Progressively narrows the threshold to equal the distance of the best match found so far, reducing run time.

Updated to correct the algorithm. (sorry!)

Optional behaviors include an upper-bound threshold distance, detect first or multiple matches of equal distance, case insensitivity.


>> [i,d]=strnearest({'first string'},{'string 2','abcdefgh','FURSrtd','firststring'})

i =


d =


>> [i,d]=strnearest({'1','first string'},{'string 2','abcdefgh','1st string','FURSrtd','seconaaad string','2'})

i =

    [6] [3]

d =

     1 5



Calculation Of Distance Between Strings inspired this file.

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
03 May 2014 Yung-Yeh

Yung-Yeh (view profile)

Oh...I see

It's actually the Levenshtein distance. The title "nearest-matching string" really confuses me.

03 May 2014 Yung-Yeh

Yung-Yeh (view profile)

This is weird, I tried

[x,d] = strnearest('ABC',{'""','"ABC"'})

but it return
x = [1 2]
d = 3

16 Aug 2013 Brandon Kuczenski

apologies- I have uploaded a corrected submission.

@@ -98,13 +98,16 @@
- luma=numel(key); lima=numel(bb);
- dl=dist*ones([luma+1,lima+1]);
- dl(1,:)=0:lima; dl(:,1)=0:luma;
+ eql=char({key;bb}); % equal length
+ keyy=eql(1,:); % space-padded key
+ bb=eql(2,:); % space-padded candidate
+ luma=numel(keyy);
+ dl=dist*ones([luma+1,luma+1]);
+ dl(1,:)=0:luma; dl(:,1)=0:luma;
for i=1:luma
- for j=max([1,i-dist]):min([lima,i+dist])
- kr=krk*(~(key(j)==bb(i)));
+ for j=max([1,i-dist]):min([luma,i+dist])
+ kr=krk*(~(keyy(min([j luma]))==bb(i)));

Comment only
16 Aug 2013 lala

lala (view profile)

Still no working example? :)

Comment only
10 Sep 2012 Sven

Sven (view profile)

Was this tested?

>> [index,distance] = strnearest({'first string'},{'string 2'})

index =


distance =


Perhaps a working example would be useful.

Comment only
26 Jun 2012 1.1

Add link to strdist.m

19 Aug 2013 1.2

Updated to correct an error in the code and to add working examples to the description.

Contact us