read collection or sequence of spreadsheet files -凯发k8网页登录
when you have data stored across multiple spreadsheet files, use spreadsheetdatastore
to manage and import the data. after creating the datastore, you can read all the data from the collection simultaneously, or you can read one file at a time.
data
if the folder c:\data
contains a collection of spreadsheet files, then capture the location of the data in location
. the data used in this example contains 10
spreadsheet files, where each file contains 10
rows of data. your results will differ based on your files and data.
location = 'c:\data';
dir(location)
. .. file01.xls file02.xls file03.xls file04.xls file05.xls file06.xls file07.xls file08.xls file09.xls file10.xls
create datastore
create a datastore using the location of the files.
ds = spreadsheetdatastore(location)
ds = spreadsheetdatastore with properties: files: { 'c:\data\file01.xls'; 'c:\data\file02.xls'; 'c:\data\file03.xls' ... and 7 more } alternatefilesystemroots: {} sheets: '' range: '' sheet format properties: numheaderlines: 0 readvariablenames: true variablenames: {'lastname', 'gender', 'age' ... and 7 more} variabletypes: {'char', 'char', 'double' ... and 7 more} properties that control the table returned by preview, read, readall: selectedvariablenames: {'lastname', 'gender', 'age' ... and 7 more} selectedvariabletypes: {'char', 'char', 'double' ... and 7 more} readsize: 'file'
read data from datastore
use the read
or readall
functions to import the data from the datastore. if the data from the collection fits in the memory, then you can import it all at once using the readall
function.
alldata = readall(ds); size(alldata)
ans = 1×2
100 10
alternatively, you can import the data one file at a time using the read
function. to control the amount of data imported, before you call read
, adjust the readsize
property of the datastore. you can set the readsize
to 'file'
, 'sheet'
, or a positive integer.
if
readsize
is'file'
, then each call toread
returns data one file at a time.if
readsize
is'sheet'
, then each call toread
returns data one sheet at a time.if
readsize
is a positive integer, then each call toread
returns the number of rows specified byreadsize
, or fewer if it reaches the end of the data.
ds.readsize = 'file'; firstfile = read(ds) % reads first file
firstfile=10×10 table
lastname gender age location height weight smoker systolic diastolic selfassessedhealthstatus
__________ ________ ___ ___________________________ ______ ______ _______ ________ _________ ________________________
'smith' 'male' 38 'county general hospital' 71 176 'true' 124 93 'excellent'
'johnson' 'male' 43 'va hospital' 69 163 'false' 109 77 'fair'
'williams' 'female' 38 'st. mary's medical center' 64 131 'false' 125 83 'good'
'jones' 'female' 40 'va hospital' 67 133 'false' 117 75 'fair'
'brown' 'female' 49 'county general hospital' 64 119 'false' 122 80 'good'
'davis' 'female' 46 'st. mary's medical center' 68 142 'false' 121 70 'good'
'miller' 'female' 33 'va hospital' 64 142 'true' 130 88 'good'
'wilson' 'male' 40 'va hospital' 68 180 'false' 115 82 'good'
'moore' 'male' 28 'st. mary's medical center' 68 183 'false' 115 78 'excellent'
'taylor' 'female' 31 'county general hospital' 66 132 'false' 118 86 'excellent'
secondfile = read(ds) % reads second file
secondfile=10×10 table
lastname gender age location height weight smoker systolic diastolic selfassessedhealthstatus
__________ ________ ___ ___________________________ ______ ______ _______ ________ _________ ________________________
'anderson' 'female' 45 'county general hospital' 68 128 'false' 114 77 'excellent'
'thomas' 'female' 42 'st. mary's medical center' 66 137 'false' 115 68 'poor'
'jackson' 'male' 25 'va hospital' 71 174 'false' 127 74 'poor'
'white' 'male' 39 'va hospital' 72 202 'true' 130 95 'excellent'
'harris' 'female' 36 'st. mary's medical center' 65 129 'false' 114 79 'good'
'martin' 'male' 48 'va hospital' 71 181 'true' 130 92 'good'
'thompson' 'male' 32 'st. mary's medical center' 69 191 'true' 124 95 'excellent'
'garcia' 'female' 27 'va hospital' 69 131 'true' 123 79 'fair'
'martinez' 'male' 37 'county general hospital' 70 179 'false' 119 77 'good'
'robinson' 'male' 50 'county general hospital' 68 172 'false' 125 76 'good'
see also
|