load files from subdirectories

조회 수: 6 (최근 30일)
Abdul Rauf Anwar
Abdul Rauf Anwar 2013년 3월 5일
답변: Lauryn Hoch 2018년 5월 14일
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