Problem with importdata, unable to open file.
Mostrar comentarios más antiguos
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
el 12 de Abr. de 2017
I had the same problem. It seems that the filename should have only alphabets
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...
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre File Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!