Documentation Center

  • Trial Software
  • Product Updates

Import or Export a Sequence of Files

To import or export multiple files, create a control loop to process one file at a time. When constructing the loop:

  • To build sequential file names, use sprintf.

  • To find files that match a pattern, use dir.

  • Use function syntax to pass the name of the file to the import or export function. (For more information, see Command vs. Function Syntax.)

For example, to read files named file1.txt through file20.txt with importdata:

numfiles = 20;
mydata = cell(1, numfiles);

for k = 1:numfiles
  myfilename = sprintf('file%d.txt', k);
  mydata{k} = importdata(myfilename);
end

To read all files that match *.jpg with imread:

jpegFiles = dir('*.jpg'); 
numfiles = length(jpegFiles);
mydata = cell(1, numfiles);

for k = 1:numfiles 
  mydata{k} = imread(jpegFiles(k).name); 
end
Was this topic helpful?