Batch file: how to access .mat files in different folders?

5 visualizaciones (últimos 30 días)
Lisa
Lisa el 17 de Jul. de 2014
Respondida: hajira ashiq el 25 de Jun. de 2018
Hi all I want to write a batch file.
  1. The code should access .mat files in different folders (which are located in one joint folder). All .mat files have the same names.
  2. Then the data should be processed.
The second step is clear. However, I have problems accessing the .mat files in the different folders. Below is what I have so far. Could you support me to solve this issue?
Thank you in advance. Lisa
----------------------------------
d = dir('C:\Users\lisa\Documents\DATA\');
isub = [d(:).isdir];
FileList = d('*_stroop_MES_Probe1.mat');
N = size(FileList,1);
for k = 1:N
% get the file name:
filename = FileList(k).name
disp(filename);
% nirs_data = load('-mat');
% insert your script code here:
importfile(filename)
A=(nirs_data.oxyData(:,:))
x=nirs_data.vector_onset(:,:)
..........
end
------------------------------------------------

Respuesta aceptada

Lisa
Lisa el 17 de Jul. de 2014
Hi
thanks again for giving me the hint. lisa
  2 comentarios
Matz Johansson Bergström
Matz Johansson Bergström el 17 de Jul. de 2014
Ah, I just uploaded my answer, too bad.
Image Analyst
Image Analyst el 27 de Jul. de 2014
Lisa, even though you can only "Accept" one answer, you can still "Vote" for his answer to give him reputation points.

Iniciar sesión para comentar.

Más respuestas (5)

Matz Johansson Bergström
Matz Johansson Bergström el 17 de Jul. de 2014
I would suggest that you use dir with the argument '*_stroop_MES_Probe1.mat' directly instead of picking from d.
For instance, I wrote d = dir('test*.mat');
m
>> d.name
ans =
test.mat
ans =
test2.mat
That is, all the mat-files beginning with the string "test". You could use regexp, but I believe that is overkill in this case.

Matz Johansson Bergström
Matz Johansson Bergström el 17 de Jul. de 2014
Editada: Matz Johansson Bergström el 17 de Jul. de 2014
I assume that all your .mat files are at a certain level (one "folder" down) in the file tree. This could be modified to suit your needs, I only print out the file names to show how it can be done.
d = dir('.');
directories = dir(str);
%go through all dirs
for i = 3:length(directories) %ignore . and ..
current_dir = directories(i);
if (current_dir.isdir)
cd(current_dir.name); %move to this directory and list all files
fprintf(1, ' Stepped into directory %s/\n', current_dir.name)
d = dir('g*.m'); %list all .m files starting with g
for i = 1:length(d)
fprintf(1, 'Found the file %s\n', d(i).name)
end
%disp('moving up')
cd('..') %go back up
end
end
This code will print out the directories it visits and print the file names matching a criteria.
I hope this helps.
And don't write a new "answer" for a response, instead give comments on my answer, please.

Lisa
Lisa el 17 de Jul. de 2014
Hi
Thank you for your answer. I tested your suggestion. It only finds the files in the dir folder.
However, this doesn't find the specified .mat files in the subfolders. How can we access those?
thanks Lisa
  1 comentario
Matz Johansson Bergström
Matz Johansson Bergström el 17 de Jul. de 2014
I would access each directory in my pwd and simply loop through those. You can do this to a certain "depth" of your file tree. If you do not know how deep your file tree is, you could resursively step through all directories and find all files in that way. I found something that might help: http://www.mathworks.se/matlabcentral/fileexchange/19550-recursive-directory-listing

Iniciar sesión para comentar.


Lisa
Lisa el 17 de Jul. de 2014
Hi Thanks, great, yes I see the files now. I would need one last help in creating a filelist. Something like:
fileList=('*_stroop_MES_Probe1.mat') % to access those .mat file specifically
I stuck here. best Lisa

hajira ashiq
hajira ashiq el 25 de Jun. de 2018
aoa everyone . i need a sample code that calls or aces files which are in different folder . when i run this file ... the inner all files are sequentialy runs

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