load files from subdirectories

2 visualizaciones (últimos 30 días)
Abdul Rauf Anwar
Abdul Rauf Anwar el 5 de Mzo. de 2013
Respondida: Lauryn Hoch el 14 de Mayo de 2018
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 comentario
Jan
Jan el 5 de Mzo. de 2013
Editada: Jan el 5 de Mzo. de 2013
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.

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 5 de Mzo. de 2013
Editada: Jan el 5 de Mzo. de 2013
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 comentarios
Abdul Rauf Anwar
Abdul Rauf Anwar el 5 de Mzo. de 2013
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 el 5 de Mzo. de 2013
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

Iniciar sesión para comentar.


Lauryn Hoch
Lauryn Hoch el 14 de Mayo de 2018
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.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by