Why does fopen fail to find a file?

I'm using fopen within a function. The name of the file to be opened is passed in the function argument, so it looks like:
function [x y z] = myfun(finp)
...
[fid msg] = fopen(finp,'rt')
The file exists, but I get the 'No such file or directory' message. However, if I just enter fopen(finp,'rt') at the command line (after having run the main code, so finp is defined exactly as passed to the function), I get no error and a positive fid value.
The file itself is in a remote directory, eg ~/Dir1/Dir2/file. If, inside the function, I cd to Dir1, then fopen('Dir2/file','rt'), it works. However, I'd rather not have to do this if possible.
Any suggestions?

답변 (3개)

Jan
Jan 2013년 8월 30일

1 개 추천

Of course you have to define the folder in which the file is found, if this is not the current folder.
finp = fullfile('~/Dir1/Dir2/', file);
[x,y,z] = myfun(finp)
There is no other way, because different folders can contain files with the same name. To solve such ambiguities, the folder is required to define the path to the file uniquely.

댓글 수: 1

Will Graham
Will Graham 2013년 8월 30일
This isn't the problem; finp contains the full pathname of the file. When I run fopen(finp,'rt') successfully from the command line, I'm doing so in the same (remote) working directory as the function that generates the error works in.

댓글을 달려면 로그인하십시오.

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 30일

0 개 추천

You have to specify the folder and the filename, for example
folder='E:\matlab'
file='yourfilename'
finp=fullfile(folder,file)
[x y z] = myfun(finp)

댓글 수: 2

Will Graham
Will Graham 2013년 8월 30일
Please see the comment that I've added to the first answer.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 30일
Post your function and how your are calling it

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2013년 8월 30일

0 개 추천

Are you using the '~' as part of the string you are passing in? ~ is a shell-level shortcut, not an operating-system shortcut, so routines such as fopen() are allowed to not understand it (or to understand it inconsistently) unless they are documented to know about it.

댓글 수: 3

Will Graham
Will Graham 2013년 8월 30일
I was, but I've now tried with the absolute pathname and found the same behaviour. The key difference between working/not working appears to be whether fopen is called from command line (or a script), or from within a function.
Walter Roberson
Walter Roberson 2013년 8월 30일
To check: you have displayed the filename and current directory just before the fopen() in each case and verified them to be the same ?
Jan
Jan 2013년 8월 30일
@Will: I do not believe that such a magic behavior is the reason for the problems. Either there is a typo or your program performs some important steps you did not explain yet, e.g. logging on to an external server or things like that.
What does this mean exactly: "I'm doing so in the same (remote) working directory as the function that generates the error works in."?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

제품

태그

질문:

2013년 8월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by