load files from subdirectories

2 次查看(过去 30 天)
Abdul Rauf Anwar
Abdul Rauf Anwar 2013-3-5
I am trying to open file BG.mat, which is present in most of the subfolders. And i want to load contents of this file in workspace. I tried using the following code, its second last line is giving me problem. Any comments would be appreciated. Thanks
filetofind = 'BG.mat';
dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
tf = ismember( {dirinfo.name}, {'.', '..'});
dirinfo(tf) = []; %remove current and parent directory.
numsubdir = length(dirinfo);
lastmod = inf * ones(numsubdir,1);
for K = 1 : numsubdir
subdirinfo = dir(fullfile(dirinfo(K).name, filetofind));
load subdirinfo.name
end
  1 个评论
Jan
Jan 2013-3-5
编辑:Jan 2013-3-5
I have formatted your code. Please apply code formatting in your posts. Thanks.
Please do not post, that is is "giving you a problem", but post the error message or explain the difference between the results and your expectations.

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2013-3-5
编辑:Jan 2013-3-5
Although I have to guess the error message, this command does not do what you expect:
load subdirinfo.name
This loads the file 'subdirinfo.name', but you want to load the file, whose name is stored in this variable:
load(subdirinfo.name);
Some minutes ago I've mentioned, that the number of users suffering under the non-functional form of SAVE (and LOAD) is decreasing. But now this confusing feature hit another user.
Remark: Loading MAT files directly to the workspace might cause serious bugs. Imagine a MAT file contains a variable called 'dirinfo'. Then the program will fail with an error (if you are lucky), or perform unwanted actions. It is much safer to catch the output in an array or struct:
Data{k} = load(...)
  2 个评论
Abdul Rauf Anwar
Abdul Rauf Anwar 2013-3-5
Hi Jan Simon Thanks for your reply. i will keep this in mind. I used the following code after incorporating your suggestion but i am still facing the error. Let me explain what i expect the code the do. I have BG.mat in all subfolders. What i want this code to do is to locate BG.mat from all subfolders of current directory, load it so that i can plot its contents in different subplots. This is the code i used
filetofind = 'BG.mat';
dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
tf = ismember( {dirinfo.name}, {'.', '..'});
dirinfo(tf) = []; %remove current and parent directory.
numsubdir = length(dirinfo);
lastmod = inf * ones(numsubdir,1);
for K = 1 : numsubdir
subdirinfo = dir(fullfile(dirinfo(K).name, filetofind));
load(subdirinfo.name);
end
Error message i get it is as follows
??? Error using ==> load Unable to read file BG.mat: No such file or directory.
Error in ==> test189 at 24 load(subdirinfo.name);
And what exactly are contents of subdirinfo are as below:
>> subdirinfo
subdirinfo =
name: 'BG.mat'
date: '04-Mrz-2013 12:54:23'
bytes: 367989
isdir: 0
datenum: 7.3530e+005
>> subdirinfo.name
ans =
BG.mat
>>
Thanks in antcipation for any help.
Jan
Jan 2013-3-5
Nicer:
inf(numsubdir,1); % Instead of: inf * ones(numsubdir,1);
The load() command requires the full path of the MAT file, otherwise it searches in the current directory.
for K = 1 : numsubdir
load(fullfile(dirinfo(K).name, filetofind));
end

请先登录,再进行评论。


Lauryn Hoch
Lauryn Hoch 2018-5-14
Your use of ismember is returning folders that are only named . and .. (the current and parent directory). tf is a logical array with two 'true' elements and everything else is false, which means you are not searching through real folders to find your file.

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by