Main Content

スプレッドシート ファイルのコレクションまたはシーケンスからの読み取り

複数のスプレッドシート ファイルにデータが保存されている場合は、spreadsheetDatastore を使用してデータの管理とインポートを行います。データストアを作成した後に、コレクションからデータをすべて同時に読み取ることも、1 つずつファイルを読み取ることもできます。

データ

フォルダー C:\Data にスプレッドシート ファイルのコレクションが含まれている場合は、location でデータの位置を取得します。この例で使用されるデータには 10 個のスプレッドシート ファイルが含まれ、各ファイルには 10 行のデータが含まれています。結果は、ファイルとデータによって異なります。

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  

データストアの作成

ファイルの場所を使用してデータストアを作成します。

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 または readall を使用して、データストアからデータをインポートします。コレクションのデータがメモリに収まる場合は、関数 readall を使用してすべて一度にインポートできます。

allData = readall(ds);
size(allData)
ans = 1×2

   100    10

あるいは、関数 read を使用して、ファイルごとにデータをインポートできます。インポートするデータ量を制御するには、read を呼び出す前にデータストアの ReadSize プロパティを調整します。ReadSize は、'file''sheet'、または正の整数に設定できます。

  • ReadSize'file' の場合、read の各呼び出しでファイルごとのデータが返される。

  • ReadSize'sheet' の場合、read の各呼び出しでシートごとのデータが返される。

  • ReadSize が正の整数の場合、read の各呼び出しで、ReadSize で指定される行数 (あるいは、データの最後に到達する場合はそれ未満) が返される。

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'            

参考

|

関連するトピック