I have solved this in following way. This is the solution i am using.
fid = fopen('Tiny_Corpus.txt','r');
tline = fgets(fid);
trigram={};
while ischar(tline)
words=regexp(tline,'|\w*|','match');
trigram1=cellfun(@(x,y,z) [x ' ' y ' ' z],words(1:end-2), words(2:end-1),words(3:end),'un',0);
trigram = [trigram trigram1];
tline = fgets(fid);
end
fclose(fid);
uniquetrigram = unique(trigram);
counts = cellfun(@(x)sum(strcmp(x,trigram)), uniquetrigram);
% Group them together
trigramcount = [uniquetrigram.' num2cell(counts.')]';