0

I have a folder with .mat files, and I want to write a loop for loading these files and doing some actions with data:

1) Choose my folder of data files

2) Perform the following set of operations (pseudocode):

for i = 1:99
    load 'Data0i.mat' ('Datai.mat', if i > 9);
    data = data * 10;
    save data as 'Data0i.mat' to another folder;
end;

What's the MATLAB implementation?

ephsmith
  • 398
  • 3
  • 12
myname
  • 137
  • 1
  • 7
  • Well, it is not matlab implementation, it's just an algorithm. I have problems with strings. How should I change data folders and how to provide numbers in names of files? – myname May 15 '12 at 12:33
  • there is `sprintf()` in matlab. you might also need `dir()` for getting the file list. – Pavel May 15 '12 at 12:35
  • possible duplicate of [Matlab file name with zero-padded numbers](http://stackoverflow.com/questions/14213442/matlab-file-name-with-zero-padded-numbers) – Shai Apr 21 '13 at 06:15

1 Answers1

1
inputFolder = 'infolder';
outputFolder = 'outfolder';

for i = 1:99
    %# Load data
    inputFilename = sprintf('%s/%02d.mat', inputFolder, i);
    load(inputFilename)

    %# Process data
    data = data * 10;

    %# Savedata
    outputFilename = sprintf('%s/%02d.mat', outputFolder, i);
    save(outputFilename, 'data')
end
petrichor
  • 6,459
  • 4
  • 36
  • 48