How to load files identified by the matlab function of "dir"?

I have many files in a folder named "mat_files". What I want to do is to load the files and get their values. Below is my code:
a = dir('mat_files');
for i = 3:length(a);
filename = a(i).name;
load strcat('mat_files/', filename);
end;
This is the error:
Error using load
Unable to read file 'filename': no such file or directory.
When I type a(3).name in the command window, I get the correct filename of "ex0219.mat". Of course, I can load the data by typing "load ex0219.mat". Clearly load a(3).name does not work. I also tried load (a(3).name), which does not work either.

댓글 수: 3

sixwwwwww
sixwwwwww 2013년 12월 8일
편집: sixwwwwww 2013년 12월 8일
You should give full path in dir if the MATLAB current folder is not same as the folder you want to access and also specify which file types you want to read (See my anwser below) otherwise MATLAB will read file information for all files in that folder and will be difficult for you to read/load few required files later on
Leon
Leon 2013년 12월 8일
편집: Leon 2013년 12월 8일
Thank you so much for the tip. I missed the path of the files when using load. But why
load strcat('mat_files/', filename) ;
does not work either?
rewrite it like this:
load(strcat('mat_files/', filename));

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2013년 12월 8일
A smiple modification gives you the answer:
myFolder = 'C:\users\leon\documents'; % or whatever.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
storedStructure = load(fullFileName);
% Now do whatever you need to do with the structure you recalled.
end

댓글 수: 1

Leon
Leon 2013년 12월 8일
편집: Leon 2013년 12월 8일
Thank you for the code. Glad to learn the use of "fullfile". It works flawlessly!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

sixwwwwww
sixwwwwww 2013년 12월 8일
편집: sixwwwwww 2013년 12월 8일
you should read files like this:
directoryString = 'YourDirectory';
files = dir(strcat(directoryString,'*.mat'));
names = {files.name}
I hope it helps. Good luck!

댓글 수: 8

Leon
Leon 2013년 12월 8일
편집: Leon 2013년 12월 8일
Thank you for the reply. The step of identifying filenames works fine. What gave me trouble is the step of loading the files. So how do I load the files with the names of "names"?
you can do it as follow:
for i = 1:length(names)
load(strcat(directoryString, names{i}))
end
Is it working now?
Leon
Leon 2013년 12월 8일
편집: Leon 2013년 12월 8일
Thank you! Unfortunately, I still get an error, after I tried:
load (strcat('mat_files/', filename));
===
Argument must contain a string.
Error in line 18: load (strcat('mat_files/', filename));
what you get by following command:
class(filename)
Leon
Leon 2013년 12월 8일
편집: Leon 2013년 12월 8일
>> class(filename)
ans =
cell
>> strcat('mat_files/', filename)
ans =
'mat_files/ex0209a.mat'
that's the problem. It should be 'char' type. Can you check what is within filename. Try
filename{1} and what you get?
Leon
Leon 2013년 12월 8일
편집: Leon 2013년 12월 8일
>> filename{1}
ans =
ex0209a.mat
After I replace filename with filename{1}, it is working now. Thank you so much!
you are wlecome

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

태그

질문:

2013년 12월 8일

댓글:

2013년 12월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by