Problem with importdata, unable to open file.

10 visualizaciones (últimos 30 días)
Ritankar Das
Ritankar Das el 31 de Mayo de 2016
Editada: dpb el 12 de Abr. de 2017
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 comentarios
Roger
Roger el 12 de Abr. de 2017
I had the same problem. It seems that the filename should have only alphabets
dpb
dpb el 12 de Abr. de 2017
Editada: dpb el 12 de Abr. de 2017
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...

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 31 de Mayo de 2016
Editada: dpb el 31 de Mayo de 2016
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...
  1 comentario
Ritankar Das
Ritankar Das el 31 de Mayo de 2016
Thanks for your help.
It seems to be working. Little bit of coding left.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by