Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How do I retrieve the filename from fileID?

Subject: How do I retrieve the filename from fileID?

From: Aurelien Queffurust

Date: 27 Jan, 2011 11:07:03

Message: 1 of 10

When you open a file you type:
>>fileID = fopen(filename)

Is there a way to retrieve the value of filename from fileID?

Thanks,
Aurélien
http://blog.developpez.com/matlab4geek/

Subject: How do I retrieve the filename from fileID?

From: Bruno Luong

Date: 27 Jan, 2011 11:26:04

Message: 2 of 10

"Aurelien Queffurust" wrote in message <ihrjkn$4in$1@fred.mathworks.com>...
> When you open a file you type:
> >>fileID = fopen(filename)
>
> Is there a way to retrieve the value of filename from fileID?
>
> Thanks,
> Aurélien
> http://blog.developpez.com/matlab4geek/

Please see this thread, especially the last post from "us":
http://www.mathworks.com/matlabcentral/newsreader/view_thread/277290

Bruno

Subject: How do I retrieve the filename from fileID?

From: Aurelien Queffurust

Date: 27 Jan, 2011 11:37:03

Message: 3 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ihrkoc$fdj$1@fred.mathworks.com>...
> "Aurelien Queffurust" wrote in message <ihrjkn$4in$1@fred.mathworks.com>...
> > When you open a file you type:
> > >>fileID = fopen(filename)
> >
> > Is there a way to retrieve the value of filename from fileID?
> >
> > Thanks,
> > Aurélien
> > http://blog.developpez.com/matlab4geek/
>
> Please see this thread, especially the last post from "us":
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/277290
>
> Bruno

Wow great ! It is exactly what I wanted :

 fnam=System.IO.DirectoryInfo(fopen(fileID)).FullName

Thanks a lot Bruno!

Subject: How do I retrieve the filename from fileID?

From: Aurelien Queffurust

Date: 27 Jan, 2011 12:11:04

Message: 4 of 10

"Aurelien Queffurust" wrote in message <ihrlcv$qdg$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ihrkoc$fdj$1@fred.mathworks.com>...
> > "Aurelien Queffurust" wrote in message <ihrjkn$4in$1@fred.mathworks.com>...
> > > When you open a file you type:
> > > >>fileID = fopen(filename)
> > >
> > > Is there a way to retrieve the value of filename from fileID?
> > >
> > > Thanks,
> > > Aurélien
> > > http://blog.developpez.com/matlab4geek/
> >
> > Please see this thread, especially the last post from "us":
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/277290
> >
> > Bruno
>
> Wow great ! It is exactly what I wanted :
>
> fnam=System.IO.DirectoryInfo(fopen(fileID)).FullName
>
> Thanks a lot Bruno!



But unfortunately under Linux the above command does not work

??? Undefined variable "System" or class "System.IO.DirectoryInfo".

Does someone know which command to use under Linux?

Thanks again for your efforts.

Subject: How do I retrieve the filename from fileID?

From: Loren Shure

Date: 27 Jan, 2011 13:23:28

Message: 5 of 10


"Aurelien Queffurust" <tug83@yahoo.fr> wrote in message
news:ihrjkn$4in$1@fred.mathworks.com...
> When you open a file you type:
>>>fileID = fopen(filename)
>
> Is there a way to retrieve the value of filename from fileID?
>

I believe you can get it by calling fopen again on the fileID.

--
Loren
http://blogs.mathworks.com/loren/
http://www.mathworks.com/matlabcentral/newsreader/search_results?search_string=tag%3Afaq

Subject: How do I retrieve the filename from fileID?

From: Aurelien Queffurust

Date: 27 Jan, 2011 13:39:04

Message: 6 of 10

"Loren Shure" <loren.shure@mathworks.com> wrote in message <ihrrkg$74s$1@fred.mathworks.com>...
>
> "Aurelien Queffurust" <tug83@yahoo.fr> wrote in message
> news:ihrjkn$4in$1@fred.mathworks.com...
> > When you open a file you type:
> >>>fileID = fopen(filename)
> >
> > Is there a way to retrieve the value of filename from fileID?
> >
>
> I believe you can get it by calling fopen again on the fileID.
>
> --
> Loren
> http://blogs.mathworks.com/loren/
> http://www.mathworks.com/matlabcentral/newsreader/search_results?search_string=tag%3Afaq


Great Great!

Thanks Loren!!! This solution is perfect and easy !

It definetly works :
filename = fopen(fileID )

and it works for all OS and moreover it can be deployed!!

Subject: How do I retrieve the filename from fileID?

From: Think blue, count two.

Date: 27 Jan, 2011 17:39:03

Message: 7 of 10

On 27/01/11 7:39 AM, Aurelien Queffurust wrote:

> Thanks Loren!!! This solution is perfect and easy !
>
> It definetly works :
> filename = fopen(fileID )
>
> and it works for all OS and moreover it can be deployed!!

It does, however, suffer from the difficulty that the returned file name
will be the name that was provided to fopen(). If a relative path was
provided to fopen() and you have changed working directories in
arbitrary ways, then it might not be possible to find the full path.

Subject: How do I retrieve the filename from fileID?

From: Loren Shure

Date: 28 Jan, 2011 14:41:42

Message: 8 of 10


"Think blue, count two." <roberson@hushmail.com> wrote in message
news:Y4i0p.17127$be.15790@newsfe05.iad...
> On 27/01/11 7:39 AM, Aurelien Queffurust wrote:
>
>> Thanks Loren!!! This solution is perfect and easy !
>>
>> It definetly works :
>> filename = fopen(fileID )
>>
>> and it works for all OS and moreover it can be deployed!!
>
> It does, however, suffer from the difficulty that the returned file name
> will be the name that was provided to fopen(). If a relative path was
> provided to fopen() and you have changed working directories in arbitrary
> ways, then it might not be possible to find the full path.
>

A best practice would be to use fullfile when you do the first fopen to
avoid this issue.

--
Loren
http://blogs.mathworks.com/loren/
http://www.mathworks.com/matlabcentral/newsreader/search_results?search_string=tag%3Afaq

Subject: How do I retrieve the filename from fileID?

From: Jan Simon

Date: 28 Jan, 2011 19:58:04

Message: 9 of 10

Dear Loren,

> A best practice would be to use fullfile when you do the first fopen to
> avoid this issue.
FULLFILE must be fed with the complete path in parts, to get the complete path as string.

I'm using a tiny function which wraps FOPEN and perform the standard checks:
  function [FID, FullName] = SafeFopen(FileName, Perm, NumFormat)
  FullName = GetFullPath(FileName); % FEX: #28249
  switch nargin
  ... Define defaults: Perm = 'r'
  ... NumFormat = 'l'
  end
  FID = fopen(FullName, Perm, NumFormat);
  if FID < 0
    error([ErrorID], 'Cannot open: %s', FullName);
  end
  return;

In addition you can insert a pattern matching using DIR, if the name contains a * or ?. And an extra error message is helpful, if a write protected file cannot be opened for writing. And finally it might be helpful to check, if the file was opened before already by searching FID in FOPEN('all') - otherwise the file pointers may move unexpectedly.

Kind regards, Jan

Subject: How do I retrieve the filename from fileID?

From: Loren Shure

Date: 28 Jan, 2011 20:21:05

Message: 10 of 10


"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
news:ihv74c$enh$1@fred.mathworks.com...
> Dear Loren,
>
>> A best practice would be to use fullfile when you do the first fopen to
>> avoid this issue.
> FULLFILE must be fed with the complete path in parts, to get the complete
> path as string.
>
> I'm using a tiny function which wraps FOPEN and perform the standard
> checks:
> function [FID, FullName] = SafeFopen(FileName, Perm, NumFormat)
> FullName = GetFullPath(FileName); % FEX: #28249
> switch nargin
> ... Define defaults: Perm = 'r'
> ... NumFormat = 'l'
> end
> FID = fopen(FullName, Perm, NumFormat);
> if FID < 0
> error([ErrorID], 'Cannot open: %s', FullName);
> end
> return;
>
> In addition you can insert a pattern matching using DIR, if the name
> contains a * or ?. And an extra error message is helpful, if a write
> protected file cannot be opened for writing. And finally it might be
> helpful to check, if the file was opened before already by searching FID
> in FOPEN('all') - otherwise the file pointers may move unexpectedly.
>
> Kind regards, Jan
>

Sure - it depends what you're trying to accomplish, what context you are
using the files in, how bullet-proof the code needs to be, ...

--
Loren
http://blogs.mathworks.com/loren/
http://www.mathworks.com/matlabcentral/newsreader/search_results?search_string=tag%3Afaq

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us