How to load all data files of all subfolders ?
43 次查看(过去 30 天)
显示 更早的评论
I save my data files in multiple subfolders of one folder. The one data folder (containing the subfolders) and the Matlab script to analyze the data are stored in the same directory. I'm stuggeling with reading the data from the subfolders.
First I tried to read only data from one folder. It worked fine:
path = 'C:\Users\Marcus\Documents\ETH\Matlab\Calculation_of_Z2S_and_Standard_Deviation\data\'; % path to one subfolder where all data is stored
liste = dir(strcat(path,'*.txt')); % creates a string with path and adds '*.txt'
files = {liste.name};
for k = 1:numel(files);
z{k} = load(fullfile(path,files{k}));
p(k) = STDfun(z{k}); % function STDfun analysis my data
end
Then I tried to modify this code to load also the data in the subfolders. As it was recommended in other posts I used genpath, but it did not work:
originalpath = 'C:\Users\Marcus\Documents\ETH\Matlab\Calculation_of_Z2S_and_Standard_Deviation\data';
foldername = fullfile(originalpath);
pth = genpath(foldername);
path = regexp([pth ';'],'(.*?);','tokens');
liste = cellfun(@(c)[c '*.txt'],path,'uni',false);
files = {liste.name};
for k = 1:numel(files);
z{k} = load(fullfile(path,files{k}));
p(k) = STDfun(z{k});
end
pth shows all subfolders just separated by ';'. I think there is some problem with the format afterwards. The error I get is:
Struct contents reference from a non-struct array object. Error in main_20160810_Minimalbeispiel (line 7) files = {liste.name};
Thanks for any advice!
0 个评论
回答(2 个)
Adithya Addanki
2016-8-15
Hi Holger,
I think the variable "path" in the script above has some issues as the last entry is empty.
I was thinking about the usecase and edited the script like below:
originalpath = 'C:\Trials\testfilepath';
foldername = fullfile(originalpath);
pth = genpath(foldername);
pathTest = regexp([pth ';'],'(.*?);','tokens');
for k = 1:size(pathTest,2)-1
list{k} = dir([pathTest{1,k}{:} '\*.txt']);
end
for i = 1:numel(list)
if(~isempty(list{i}))
temp = list{i};
for j = 1: numel(temp)
disp(fullfile(temp(j).folder,temp(j).name))
end
end
end
PS: It is not suggested to overload in-built functions. The script above is just an example to give a rough idea, instead you may even create a separate function that can be called as required iteratively when the list is found to be an array of file names etc.
The general workflow is given here:
I hope this helps.
Thank you,
Adithya
0 个评论
Image Analyst
2016-8-15
Holger, do NOT use path as the name of your variable - you'll overwrite the prior, existing path and that could really mess things up. path is a built-in variable that you should only mess with via the "set path" button on the home tab of the toolribbon, unless you really know what you're doing.
To split your path into separate paths (get rid of semicolons), simply use strsplit:
pth = genpath(foldername);
pth = strsplit(pth, ';') % Separate into individual folders.
pth is now a cell array. To get the kth folder, use braces:
thisFolder = pth{k}; % Extract kth folder from a cell into a regular string character array.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!