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

5 次查看(过去 30 天)
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
------------------------------------------------

采纳的回答

Lisa
Lisa 2014-7-17
Hi
thanks again for giving me the hint. lisa
  2 个评论
Image Analyst
Image Analyst 2014-7-27
Lisa, even though you can only "Accept" one answer, you can still "Vote" for his answer to give him reputation points.

请先登录,再进行评论。

更多回答(5 个)

Matz Johansson Bergström
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 2014-7-17
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 2014-7-17
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 个评论
Matz Johansson Bergström
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
Lisa 2014-7-17
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 2018-6-25
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

类别

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