MATLAB: Loading images with their names into single arrays

for loopimagesloading

Hi, I'm trying to load multiple .tif files into MATLAB from a folder. I tried this:
files= dir('*.tif')
for k=1:length(files);
imagename = files(k).name;
image1 = imread(imagename);
images{k} = image1;
end
But I need these files to be loaded separately, so every one of them will be one single array, but this code writes them all into a cell. I also need these single array variables to have the same names that .tif files had. How can I do that?

Best Answer

One of the best approaches is to simply import the file data into the same structure that dir returns, then for each file you have all of the file data and file meta-data in one simple structure element:
D = 'absolute or relative path to where the files are saved';
S = dir(fullfile(D,'*.tif'))
for k = 1:numel(S);
F = fullfile(D,S(k).name);
S(k).data = imread(F);
end
You can trivially access any file using indexing, e.g. the third file:
S(3).name
S(3).data
See also: