Combining image files into one array

I have many image files that I am trying to combine into one column array/matrix. The file names are sequentially numbered. Each image file is about 200 x 200 pixels, grayscaled (values are 0 to 255), .tif files. I need the first file's contents to be read from left to right, and top to bottom, starting with row 1 column 1. The information from all image files needs to be ordered into one column, starting with the first file.
(There are more functions that I will be applying to each image, but I am having trouble coming up with an easy way to construct the final column matrix).
Thank you for the help.

Answers (2)

imagefiles = {...}; %list of files to read, obtained any way you want, e.g. with |dir|, or |uigetfile|
imagevectors = zeros(200*200, numel(imagefiles));
for imageidx = 1:numel(imagefiles)
imagevectors(:, imageidx) = imread(imagefiles{imageidx}); %will fail if the image is not exactly 200x200
end

5 Comments

Note that you can get the correct order from the sequentially-numbered files by using my FEX submission natsortfiles:
Thank you for the help everyone. I have tried your code Guillaume and I am getting the below error:
Cell contents reference from a non-cell array object.
Error in loop (line 27) imagevectors(:, imageidx) = imread(imagefiles{imageidx})
Can you help with this? Thank you!
@Student_Coder: it seems like imagefiles is not a cell array. How did you generate imagefiles ?
They are output from a software used with laboratory equipment. They are unit8 .tif files
As per the comment I wrote, the imagefiles cell array is supposed to contain the list of files to open. I.e. something like:
imagefiles = {'C:\somewhere\somename.tif'
'C:\somewhere\someothername.tif'
'C:\whereever\readme.tif'};
Your question seemed to be about how to read the files and then store them one by column. This is what I've answered.
However, you now seem to imply that you've already read the files as uint8. If you've already read the files, then how are they all stored in matlab. Whichever way (assuming it's not each in an individual variable), it's trivial to reshape and concatenate them as one column each.

Sign in to comment.

KSSV
KSSV on 11 Apr 2017
Edited: KSSV on 11 Apr 2017
files = dir('*tif') ;
for i = 1:length(files)
disp(files(i).name)
% do waht you want
end

4 Comments

Hi, that appears to give me a list of file names, but does not create a new array composed of the content in the original files.
files = dir('*tif') ;
iwant = cell(length(files),1) ;
for i = 1:length(files)
iwant{i} = files(i).name ;
end
iwant
Hi, that is still only using file names. I am concerned with the actual content within the file. For example, each file is 200 x 200 pixels and each cell has a value from 0 to 255.
Follow the same..inside the loop...read the image using imread

Sign in to comment.

Asked:

on 11 Apr 2017

Commented:

on 12 Apr 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!