This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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);

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); 
Was this topic helpful?