Adding filenames to compiled data

조회 수: 1 (최근 30일)
Jen
Jen 2019년 3월 4일
답변: Nimit Dhulekar 2019년 3월 4일
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개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by