MATLAB: Applying function only to certain files in directory

strings

Hi, I have the following code, which applies the function 'load_nii' to files from the current directory which I select manually with the variables k and l:
currentFolder = pwd;
listing = dir;
allDirNames = { listing.name };
dirLA =~ [ listing.isdir ];
dirLA(1:2) = 0;
dirNames = allDirNames(dirLA);
% set k, l to choose which files to convert
k = 3;
l = 182;
% convert Niftis to Mat-Files
for idx = k:l
foo(idx) = load_nii(allDirNames{idx});
betas{idx-(k-1)} = foo(idx).img;
end
What I want however is that the function load_nii is automatically applied to all files which start with certain strings (e.g. "results"), such that I don't have to set the variables k and l manually. How can I do that?
Thanks

Best Answer

The simplest solution is, when you know a priori what your files are called. In that case use wildcards with *
listing = dir('results*.m');