Batch file: how to access .mat files in different folders?
Mostrar comentarios más antiguos
Hi all I want to write a batch file.
- The code should access .mat files in different folders (which are located in one joint folder). All .mat files have the same names.
- 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
Más respuestas (5)
Matz Johansson Bergström
el 17 de Jul. de 2014
2 votos
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
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
el 17 de Jul. de 2014
0 votos
1 comentario
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
Lisa
el 17 de Jul. de 2014
hajira ashiq
el 25 de Jun. de 2018
0 votos
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 Search Path en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!