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 个评论
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
sixwwwwww
2013-12-8
rewrite it like this:
load(strcat('mat_files/', filename));
采纳的回答
更多回答(1 个)
you should read files like this:
directoryString = 'YourDirectory';
files = dir(strcat(directoryString,'*.mat'));
names = {files.name}
I hope it helps. Good luck!
8 个评论
sixwwwwww
2013-12-8
you can do it as follow:
for i = 1:length(names)
load(strcat(directoryString, names{i}))
end
Is it working now?
sixwwwwww
2013-12-8
what you get by following command:
class(filename)
sixwwwwww
2013-12-8
that's the problem. It should be 'char' type. Can you check what is within filename. Try
filename{1} and what you get?
sixwwwwww
2013-12-8
you are wlecome
类别
在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!