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 Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by