-3

I have a function which takes multi-page tiff images and finds the average of the maximum pixel from each page. That function is working fine, but it takes a variable number of input pathways using the varargin function.

The inputs would have a format like 'C:\Users\me\desktop\thefolder\theimage.tif', the function takes all of these inputs and gives me the averages. I need help automating this process a bit more.

Up until now I have just been manually writing out the path for each individual image, but as the number of multipage tiff goes up it becomes time consuming to write everything out...

How do I write a function that will easily find all of the images, then give me char variables in the workspace corresponding to all of the image paths that I can then feed to the main function?

Kelly Agnew
  • 51
  • 1
  • 4
  • [Loop through files in a folder in matlab](http://stackoverflow.com/questions/11621846/loop-through-files-in-a-folder-in-matlab) – sco1 Jul 07 '16 at 19:19

1 Answers1

0

The dir function can take wildcards to return a structure array of all matching files in a single folder.

E.g.

>> files = dir('C:\Users\me\desktop\thefolder\*.tif');

files = 

3x1 struct array with fields:

    name
    date
    bytes
    isdir
    datenum

You could loop through each element of the array, or you can create a cell array in the workspace as follows:

>> tif_filenames = {files.name}

tif_filenames = 

    'theimage.tif'    'another.tif'    'and_another.tif' 

However if your processing function uses varargin, then you can simple call it as follows:

>> find_max_average_pixel(files.name)

and MATLAB maps each filename to an entry in varargin, so from inside your function it appears as if it's been called with multiple strings.

If you wish to pull files from a tree of sub-folders, then it needs a bit more thought.

Erik
  • 812
  • 1
  • 7
  • 15