MATLAB: How to keep updating and accumulating the arrays as I read multiple files one after the other

accumulationcellarray

So I have multiple m.files, and I have implemented my code which is able to read one file and do exactly what I need it to do.
However, I need to run this code over multiple files, all with different words in them, and I need to at the end of it find all existing words in ALL the files.
How can I do this.
This is my code so far
fid=fopen('testing1.m')
out=textscan(fid, '%s', 'Delimiter', '\n');
out=regexp(lower(out{1}), ' ' , 'split');
fclose(fid)
comb=unique([out{:}]);
comb=comb(~cellfun('isempty', comb));
m=size(out,1)
idx=false(m,size(comb,2));
for j=1:m
idx(j,:)=ismember(comb,out{j});
if ismember('hello', out{j})
AL(j,:)=idx(j,:);
end
end
AL(all(AL==0,2),:)=[];
end
To open up my multiples files I use this
for i=1:2
fid=fopen(sprintf('testing%d.m',i))
When I use this to open up 2 files, I can't seem to make my code work because of the matrix dimension.
Any ideas on how to output a cell array AL, for two m.files testing1 testing2? I wanna create cell arrays which accumulates each time a file is being read.

Best Answer

You could go for something like:
words = {} ;
for k = 1 : 2
buffer = fileread(sprintf('testing%d.m', k)) ;
words = [words, regexp(buffer, '\w*', 'match')] ; % Alphanumerical words.
end
uniqueWords = unique(words) ;
% .. etc.