MATLAB: Adding filenames to compiled data

excelfilefilenamefoldersimporting excel datasubfolders

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');

Best Answer

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);