Loop through a series of .mat files in MATLAB

2.1k Views Asked by At

I have 12 mat files (one for each month) that need to be used individually for a calculation. They are too big to load all twelve at one time. There fore I would like to call month #1, do the calculation, call month #2, ...#12.

I have done this with .asc files in the past, but am having trouble with. mat files.

I am trying the following:

matObj = matfile('tempOR_norm.mat')

matObj = 

  matlab.io.MatFile

  Properties:
      Properties.Source: 'C:\GIS_DATA\SNOW\Programs\SNOW\tempOR_norm.mat'
    Properties.Writable: false                                           
         tempOR_01_norm: [1424x1817 double]                              
         tempOR_02_norm: [1424x1817 double]                              
         tempOR_03_norm: [1424x1817 double]                              
         tempOR_04_norm: [1424x1817 double]                              
         tempOR_05_norm: [1424x1817 double]                              
         tempOR_06_norm: [1424x1817 double]                              
         tempOR_07_norm: [1424x1817 double]                              
         tempOR_08_norm: [1424x1817 double]                              
         tempOR_09_norm: [1424x1817 double]                              
         tempOR_10_norm: [1424x1817 double]                              
         tempOR_11_norm: [1424x1817 double]                              
         tempOR_12_norm: [1424x1817 double] 

But how do I loop through this list?

I need something along the lines of

input = load(matObj(i))

but this give me:

MatFile objects are scalar. Access variables using the syntax objName.varName(indices).

Any insight would be appreciated!

-Thanks-

2

There are 2 best solutions below

1
On

Got it (with help from Loop for loading and saving .mat files):

it goes like this...

 for i = 1:12;
        if (i <= 9)
            filename = ['tempOR_0', int2str(i), '_norm.mat'];
        else    filename = ['tempOR_', int2str(i), '_norm.mat'];
        end
 end

load(filename);
0
On

if you dont even know the file names try dir() command, if I'm recalling correctly you can specify what file extensions to return with dir(./*.mat). You can find more stuff from http://www.mathworks.com.au/help/techdoc/ref/dir.html