텍스트 파일 모음이나 일련의 텍스트 파일 읽어오기
데이터가 여러 개의 텍스트 파일에 저장되어 있는 경우 tabularTextDatastore를 사용하여 데이터를 관리하고 가져올 수 있습니다. 이 예제에서는 tabularTextDatastore를 사용하여 텍스트 파일 모음에서 데이터를 동시에 읽어오거나, 한 번에 한 파일씩 읽어오는 방법을 보여줍니다.
데이터
이 예제에서는 폴더 C:\DataTxt에 텍스트 파일 모음이 들어 있습니다. 이 위치를 변수 location에 넣어 둡니다. 데이터에는 10개의 텍스트 파일이 들어 있으며, 각 파일에는 10개 행의 데이터가 들어 있습니다. 결과는 파일과 데이터에 따라 달라집니다.
location = 'C:\DataTxt';
dir(location). File01.csv File03.csv File05.csv File07.csv File09.csv .. File02.csv File04.csv File06.csv File08.csv File10.csv
데이터저장소 만들기
파일 위치를 사용하여 데이터저장소를 만듭니다.
ds = tabularTextDatastore(location)
ds =
TabularTextDatastore with properties:
Files: {
'C:\DataTxt\File01.csv';
'C:\DataTxt\File02.csv';
'C:\DataTxt\File03.csv'
... and 7 more
}
FileEncoding: 'UTF-8'
AlternateFileSystemRoots: {}
ReadVariableNames: true
VariableNames: {'LastName', 'Gender', 'Age' ... and 7 more}
DatetimeLocale: en_US
Text Format Properties:
NumHeaderLines: 0
Delimiter: ','
RowDelimiter: '\r\n'
TreatAsMissing: ''
MissingValue: NaN
Advanced Text Format Properties:
TextscanFormats: {'%q', '%q', '%f' ... and 7 more}
TextType: 'char'
ExponentCharacters: 'eEdD'
CommentStyle: ''
Whitespace: ' \b\t'
MultipleDelimitersAsOne: false
Properties that control the table returned by preview, read, readall:
SelectedVariableNames: {'LastName', 'Gender', 'Age' ... and 7 more}
SelectedFormats: {'%q', '%q', '%f' ... and 7 more}
ReadSize: 20000 rows
데이터저장소에서 데이터 읽어오기
read 또는 readall 함수를 사용하여 데이터저장소에서 데이터를 가져옵니다. 모음의 데이터가 메모리에 다 들어갈 수 있는 양이면 readall 함수를 사용하여 데이터를 한 번에 모두 가져올 수 있습니다.
allData = readall(ds); size(allData)
ans = 1×2
100 10
또는 read 함수를 사용하여 한 번에 한 파일의 데이터를 가져옵니다. read를 호출하기 전에 가져올 데이터 양을 제어하려면 데이터저장소의 ReadSize 속성을 조정하십시오. ReadSize를 'file' 또는 양의 정수로 설정합니다.
ReadSize가'file'이면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 filesecondFile=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'
참고 항목
readtable | readmatrix | readcell | readvars | readtimetable | tabularTextDatastore