The first step would be to create a function that can generate an n-gram for a given string. One way to do this in a vectorized fashion is with some clever indexing.
function subStrings = n_gram(fullString,N)
nString = numel(fullString);
index = repmat((0:(nString-N))',1,N)+...
repmat(1:N,(nString-N+1),1);
subStrings = unique(cellstr(fullString(index)));
end
This uses the function REPMAT to first create a matrix of indices that will select each set of unique N-length substrings from the given string. Indexing the given string with this index matrix will create a character array with one N-length substring per row. The function CELLSTR then places each row of the character array into a cell of a cell array. The function UNIQUE then removes repeated substrings.
With the above function you can then easily count the number of n-grams shared between two strings using the INTERSECT function:
subStrings1 = n_gram('tool',2);
subStrings2 = n_gram('fool',2);
sharedStrings = intersect(subStrings1,subStrings2);
nShared = numel(sharedStrings);