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

조회 수: 5 (최근 30일)
Lisa
Lisa 2014년 7월 17일
답변: hajira ashiq 2018년 6월 25일
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
Matz Johansson Bergström
Matz Johansson Bergström 2014년 7월 17일
Ah, I just uploaded my answer, too bad.
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
Matz Johansson Bergström 2014년 7월 17일
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일
편집: 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
Matz Johansson Bergström 2014년 7월 17일
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