File Extensions Name-Value pair error

64 visualizaciones (últimos 30 días)
Abraham Caceres
Abraham Caceres el 8 de Dic. de 2021
Editada: Jeremy Hughes el 8 de Dic. de 2021
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

Respuesta aceptada

Jeremy Hughes
Jeremy Hughes el 8 de Dic. de 2021
Editada: Jeremy Hughes el 8 de Dic. de 2021
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.

Más respuestas (0)

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by