Adding filenames to compiled data

1 次查看(过去 30 天)
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');

采纳的回答

Nimit Dhulekar
Nimit Dhulekar 2019-3-4
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);

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by