From: Jon Harris on
In a .m script, I've opened a file with fopen. Since Matlab has various path search options, I'm not sure where the file was actually opened/created. Is there a way to find the absolute path of the file?

The file may have been specified with just a simple filename (test.txt), a relative path (..\test.txt), or an absolute path and filename (C:\test.txt). I want a way to find out the full absolute path.

I'm on Windows if that makes a difference.
From: Bruno Luong on
"Jon Harris" <jonharris7_remove(a)gmail.com> wrote in message <hob47o$a1m$1(a)fred.mathworks.com>...
> In a .m script, I've opened a file with fopen. Since Matlab has various path search options, I'm not sure where the file was actually opened/created. Is there a way to find the absolute path of the file?
>
> The file may have been specified with just a simple filename (test.txt), a relative path (..\test.txt), or an absolute path and filename (C:\test.txt). I want a way to find out the full absolute path.
>
> I'm on Windows if that makes a difference.

I ask this question while ago and I come to the conclusion is the best way is:

file='..\test.txt'
fullpath=cd(cd(fileparts(file)))

Bruno
From: Jon Harris on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hob57l$qsj$1(a)fred.mathworks.com>...
> "Jon Harris" <jonharris7_remove(a)gmail.com> wrote in message <hob47o$a1m$1(a)fred.mathworks.com>...
> > In a .m script, I've opened a file with fopen. Since Matlab has various path search options, I'm not sure where the file was actually opened/created. Is there a way to find the absolute path of the file?
> >
> > The file may have been specified with just a simple filename (test.txt), a relative path (..\test.txt), or an absolute path and filename (C:\test.txt). I want a way to find out the full absolute path.
> >
> > I'm on Windows if that makes a difference.
>
> I ask this question while ago and I come to the conclusion is the best way is:
>
> file='..\test.txt'
> fullpath=cd(cd(fileparts(file)))
>
> Bruno

This seems to work fine with relative or absolute paths. But if the file is just a name, I get this:

» file='test.txt'
file =
test.txt
» fullpath=cd(cd(fileparts(file)))
??? Name is nonexistent or not a directory
From: us on
"Jon Harris" <jonharris7_remove(a)gmail.com> wrote in message <hob47o$a1m$1(a)fred.mathworks.com>...
> In a .m script, I've opened a file with fopen. Since Matlab has various path search options, I'm not sure where the file was actually opened/created. Is there a way to find the absolute path of the file?
>
> The file may have been specified with just a simple filename (test.txt), a relative path (..\test.txt), or an absolute path and filename (C:\test.txt). I want a way to find out the full absolute path.
>
> I'm on Windows if that makes a difference.

one of the solutions

fnam='unique.m';
fp=which(fnam);
disp(fp);
% F:\usr\r2010a\toolbox\matlab\ops\unique.m

us
From: Bruno Luong on
> This seems to work fine with relative or absolute paths. But if the file is just a name, I get this:
>
> » file='test.txt'
> file =
> test.txt
> » fullpath=cd(cd(fileparts(file)))
> ??? Name is nonexistent or not a directory

Easy to fix if you check whenever fileparts returns empty

Bruno