Adding filenames to compiled data

2 visualizaciones (últimos 30 días)
Jen
Jen el 4 de Mzo. de 2019
Respondida: Nimit Dhulekar el 4 de Mzo. de 2019
I have compiled data of about 200 excel sheets and need to add the file's name before each of the data sets. How can I do this easily? My code so far is:
clear all
close all
ds = spreadsheetDatastore('C:\Users\Control', 'IncludeSubfolders', true);
idx = [];
for i=1:length(ds.Files)
if strfind(ds.Files{i},'Inflections')
idx=[idx;i];
end
end
ds.Files = ds.Files(idx);
data=ds.readall();
writetable(data, 'masterexcelfile_DR.xlsx');

Respuesta aceptada

Nimit Dhulekar
Nimit Dhulekar el 4 de Mzo. de 2019
I am not completely certain about the phrasing in your question: "add the file's name before each of the data sets". This is a potential way of getting filenames from the read of the datastore. Replace your existing call to readall with this loop. Every row of the table contains the name of the file in the first variable. The file name is repeated for all rows read from one file.
allData = [];
while hasdata(ssds)
[data, info] = read(ssds);
data.Filename(:) = string(info.Filename);
allData = [allData; data];
end
data = allData;
% move the last variable to the first variable in the table
data = movevars(data, "Filename", "Before", 1);

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by