Associate filename to datastore read data

4 次查看(过去 30 天)
Hello,
I am reading over 2000 .xls files into a datastore:
Here is the code I wrote until now:
location = 'C:\test';
ssds = spreadsheetDatastore(location,'NumHeaderLines',1);
ssds.ReadSize = 3000;
ssds.SelectedVariableNames = 'MyName';
whatsNeeded=readall(ssds);
I know have 3000 datapoints (from over 700000) of the .xls column 'MyName' in the variable whatsNeeded.
For example:
whatsNeeded(1) = 'Number1'
whatsNeeded(2) = 'Number2'
...
whatsNeeded(172) = 'Number1'
...
whatsNeede(1583) = 'Number1'
...
whatsNeeded(3000) = 'Number1'
I now need the info, from which original file/from which filename the variables (1), (172), (1583) & (3000) came.
Is there a way to associate these information?
Thank you.

回答(1 个)

James Hong
James Hong 2020-4-24
As of R2020a, there is no direct way to do this, but a workaround is to append the data with a column that identifies the filename:
ds = ...
ds = transform(ds, @addFilenameToData, 'IncludeInfo', true);
tt = tall(ds)
where 'addFilenameToData' is defined:
function [data, info] = addFilenameToData(data, info)
[~, filename, ext] = fileparts(info.Filename);
data.Filename(:, 1) = string([filename ext]);
end

类别

Help CenterFile Exchange 中查找有关 Datastore 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by