File Extensions Name-Value pair error
조회 수: 32 (최근 30일)
이전 댓글 표시
Error using spreadsheetDatastore
Input folders or files that contain non-standard file extensions
Use File Extensions Name-Value pair to include non-standard file extensions
What is missing or needs correction? I've read Matlab spreadsheetDatastore and other links but nothing helps.
myFolder = 'C:\Users\Abraham\Documents\My Documents';
filePattern = fullfile(myFolder, '**/*.csv');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, theFiles(k).name);
fprintf(1, 'Now reading %s\n', fullFileName);
str = string(k);
ssds = spreadsheetDatastore(fullFileName,"OutputType","table");
writetable(ssds, "Table" + " " + str + ".csv");
end
댓글 수: 0
채택된 답변
Jeremy Hughes
2021년 12월 8일
편집: Jeremy Hughes
2021년 12월 8일
The issue is that .csv isn't a supported spreadsheet format. Many people associate csv with spreadsheet, and that's likely due to being one of the first tabular formats to become popular. Most spreadsheet programs can import and export spreadsheet files.
The error message indicates that you should use the "FileExtensions",".csv" name value pair with spreadsheertDatastore, but that will still not work as expected. MATLAB expects spreadsheet formatted files like XLS, XLSX.
You should use tabularTextDatastore which is designed to work CSV. But that's not all you need to do.
These two lines will produce an error, and so will tabularTextDatastore
ssds = spreadsheetDatastore(fullFileName,"OutputType","table");
writetable(ssds, "Table" + " " + str + ".csv");
The writetable function expects a table, and "ssds" is a SpreadSheetDatastore. To get data out of ssds you need to use the read or readall.
You could do this much more simply by letting datastore take care of the file handling.
myFolder = 'C:\Users\Abraham\Documents\My Documents';
ds = tabularTextDatastore(myFolder,"FileExtenstions",".csv");
i = 1
% In older releases, you might need to loop, in 20a or later, see below
while hasdata(ds)
T = read(ds);
writetable(T, sprintf("Table%d.csv",i));
i = i + 1;
end
... or more simply in R2020a or later
myFolder = 'C:\Users\Abraham\Documents\My Documents';
ds = tabularTextDatastore(myFolder,"FileExtenstions",".csv");
tds = transform(ds,@myTransformFunction); % only need this if you want to make changes to the data.
writeall(ds,"outputDir/")
Without any files, it's hard to test this, but it should work with minimal modifications.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!