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.
If the folder
C:\Data contains a collection of files, then use
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
list = dir('C:\Data\*.xlsx'); numFiles = length(list)
numFiles = 10
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); end
If your data is located in specific worksheet or range, then use the
'Range' name-value pair to
specify the data location. For more information on the name-value pairs, see
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