In essence, your approach is what I would go for. However, your lines of code can be simplified (directories are lost in the regular expression and empty cells in the final concatenation):
f = dir('C:\directory'); f = regexpi({f.name},'.*txt|.*pdf','match'); f = [f{:}];
Also note that the dir() function accepts wildcards ( * ), but not several extensions:
dir('C:\directory\*.avi')
This means that you can immediately get only those files that correspond to the extension, however, a number of extensions requires a loop :
d = 'C:\users\oleg\desktop'; ext = {'*.txt','*.pdf'}; f = []; for e = 1:numel(ext) f = [f; dir(fullfile(d,ext{e}))]; end
Alternative (not recommended)
ext = {'*.txt','*.pdf'}; str = ['!dir ' sprintf('%s ',ext{:}) '/B']; textscan(evalc(str),'%s','Delimiter','')
where str !dir *.txt *.pdf /B and evalc() captures the line score, and textscan() parses it.
Oleg source share