Main Content

스프레드시트 파일 모음이나 일련의 스프레드시트 파일 읽어오기

데이터가 여러 개의 스프레드시트 파일에 저장되어 있는 경우 데이터를 관리하고 가져오려면 spreadsheetDatastore를 사용하십시오. 데이터저장소를 만들고 나면 모음에서 모든 데이터를 동시에 읽어오거나, 한 번에 하나씩 파일을 읽어올 수 있습니다.

데이터

폴더 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'            

참고 항목

|

관련 항목