This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Read Sequence of Spreadsheet Files

You can read multiple spreadsheet files from a collection and organize the data into a MATLAB® structure. To import the data, first get a complete list of file names, and then read the files one at a time.

Get File Names

If the folder C:\Data contains a collection of files, then use the dir command to gather the list of file names and display the number of files in the collection. Your results will differ based on your files and data.

list = dir('C:\Data\*.xlsx');
numFiles = length(list) 
numFiles = 10 

Read One File At a Time

Import the data one file at a time, using readtable in a for loop. The readtable function reads and returns the tabular data from the first sheet of the spreadsheet file.

for iFile = 1:numFiles
  FileName = list(iFile).name; 
  Data(iFile).FileName = FileName;
  Data(iFile).T = readtable(FileName);

If your data is located in specific worksheet or range, then use the 'Sheet' or 'Range' name-value pair to specify the data location. For more information on the name-value pairs, see readtable.

Preview the Data from File

Display the file name and the imported table for the first file. Your results will differ based on your files and data.

ans = 'File01.xlsx'
ans = 
     LastName     Age    Weight    Smoker
    __________    ___    ______    ______

    'Smith'       38     176       1     
    'Johnson'     43     163       0     
    'Williams'    38     131       0     
    'Jones'       40     133       0     
    'Brown'       49     119       0     

See Also


Related Topics

Was this topic helpful?