MATLAB: Problem with importdata, unable to open file.

data importfileimportimportdataopen fileproblemread

I have the following code:
function c=somename(fname) % fname contains a path to a folder
folder=dir(fname);
f=size(folder);
f=f(1);
for n=1:f
name=folder(n).name;
fullname{n}=fullfile(fname,cellstr(name));
filename=fullname{n};
a{n}=importdata(fullname{n}); % loads data
a{n}=a{n}.textdata;
....
end
when I am executing this function I get the following output:
Error using importdata (line 136)
Unable to open file.
Please help me to solve this problem.
Thanks in advance.
Ritankar Das

Best Answer

For reasons only known to Gates and Allen, MS dir returns the two '.' and '..' entries which aren't files (nor anything else useful) when the listing is over a subdirectory (aka "folder").
You need to skip those two entries (plus any others that might also be subdirectories) in traversing the entries --
d=dir(fname);
for n=1:length(d)
if d.isdir(n), continue, end % skip the directory entries
a{n}=importdata(fullfile(fname,d(n).name)); % loads data
...
You also had quite a number of unneeded temporaries; I dispensed with them albeit you'll need to check for typos and matching paren's, done from the keyboard...