Problem with importdata, unable to open file.

6 次查看(过去 30 天)
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
  2 个评论
Roger
Roger 2017-4-12
I had the same problem. It seems that the filename should have only alphabets
dpb
dpb 2017-4-12
编辑:dpb 2017-4-12
Well, specifically an allowable filename has rules as specified by the OS, but dir will only return elements that are found so that's not the issue. The issue here was/is that just because dir returned an entry, that does NOT mean each and every entry is a file that can be opened; some entries may be additional subdirectories (aka "folders") and, in particular, the Matlab DIR command if not qualified by a wildcard matching string or somesuch returns the two symbolic directory entries "." and ".." which are a shorthand notation for the current and parent directory, respectively. They are NOT files so trying to use their entries as such, not surprisingly it would seem, fails.
While this is consistent with Unix systems, I've yet to see that the convention has any practical utility as a return value but it is what it is and so user code must deal with it.
There is another annoyance with Matlab and fopen in particular in that it is not cellstr or the newer string enabled; you must ensure to use char or the curlies to dereference any file names that are not character string arrays else't the call will fail. Why this hasn't been fixed in 10+ yr I have no idea. :( While dir does return the .name property as a character string, its counterpart uigetfile can and does return a cell array of strings. Unfortunately, this happens only if one selects more than one file as well, if just one is returned then it is a character string. Thus, code using it must also special-case/test the return. Another pita that should've never been allowed out in the wild to begin with and really, really, really ought to be fixed...

请先登录,再进行评论。

采纳的回答

dpb
dpb 2016-5-31
编辑:dpb 2016-5-31
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...

更多回答(0 个)

类别

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