Using dir() in a compiled script

Hi everybody!
I'm trying to compile my matlab script which includes the dir function to get the files from a certain path. It works perfectly in the uncompiled *.m version but as soon as I've compiled my code as a console application it seems like that function doesn't work Here's the code:
...
path=strcat(path,'Results');
path2=strcat(path2,'\\Results');
fprintf('%s \n',path);
fprintf('%s \n', path2);
%Open file
fprintf('read files \n');
files=dir(strcat(path,'/*.txt'));
fprintf('list files \n');
fprintf(files.name);
fprintf('listed files \n');
k=1;
for i=1:length(files)
fprintf('i = %d \n',i);
filename=files(i).name;
if length(filename)>5
if strcmp(filename(1:6),'Blende')
fprintf('%s \n', filename);
filepath{k}=strcat(path2,'\\',filename);
k=k+1;
end
end
end
...
And this is the output:
Z:\...\Results
Z:\\...\\Results
read files
list files
??? Error using ==> fprintf
Not enough input arguments.
Error in ==> start at 52
MATLAB:minrhs
Z:\...\Programm>
I get the variable "path" which contains my path to the folder with my textfiles (it's on a server) over a function before. This seems to work since it prints that path.
Any idea?
Thanks!

 Respuesta aceptada

Robert Cumming
Robert Cumming el 12 de Sept. de 2014

0 votos

You get the fprintf error becuase your variable files is empty.
You should look at the var fullfile to build your path and wildcard.
You can check the path using isdir .

4 comentarios

Benjamin
Benjamin el 12 de Sept. de 2014
Thank you for your quick answer! Does the fullfile function change anything? The path is correct, it works when I just run the matlab script and the variable files gets filled with all the textfile names. So in the uncompiled programm that variable is not empty but in the compiled version. Is there a problem with using dir ?
Robert Cumming
Robert Cumming el 12 de Sept. de 2014
It does (should) work the same between compiled and not compiled.
Are any of your paths relative?
The error for fprintf is due to files being empty (test it independently) -> so therefore the string into your dir command is not the path you think it is... Or the path cant be seen (permissions?), network access etc....
Put some extra statements in your code to check your variables/paths - printing statements to the screen and you should be able to find the issue...
Benjamin
Benjamin el 12 de Sept. de 2014
Thanks! I found out, that it's got a problem with server paths, maybe the permissions. As I ran the program on a local drive it was working.
Benjamin
Benjamin el 12 de Sept. de 2014
Is there any way to handle those network permissions? The network drive is mounted...I've also tried with setting the real network path (not Z: ...) but it doesn't work either. The current user has access to that drive with his login

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Sept. de 2014

Comentada:

el 12 de Sept. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by