Thread Subject: opening files in DIFFERENT directories

Subject: opening files in DIFFERENT directories

From: Juliette Salexa

Date: 5 Jul, 2009 01:14:02

Message: 1 of 3

I am having problems with fopen.

filename=test.m
fopen(filename,'r')

works fine, but:

fopen(fullfile('directory1','directory2',filename,'r')) OR
fopen('directory1','directory2',filename,'r') OR
fopen(/directory1/directory2/filename,'r'))

do NOT. (the latter command works in UNIX but not windows).

So how do I open a file that's not in my working directory ??

Subject: opening files in DIFFERENT directories

From: TideMan

Date: 5 Jul, 2009 03:22:38

Message: 2 of 3

On Jul 5, 1:14 pm, "Juliette Salexa" <juliette.physic...@gmail.com>
wrote:
> I am having problems with fopen.
>
> filename=test.m
> fopen(filename,'r')
>
> works fine, but:
>
> fopen(fullfile('directory1','directory2',filename,'r')) OR
> fopen('directory1','directory2',filename,'r') OR
> fopen(/directory1/directory2/filename,'r'))
>
> do NOT. (the latter command works in UNIX but not windows).
>
> So how do I open a file that's not in my working directory ??

When you say that this worked in Unix:
fopen(/directory1/directory2/filename,'r'))
I think you're telling porkies.

For a start, it ends with )), which is bad syntax.
Secondly, there are no quotes around the string.
Furthermore, you need to attach a handle.
This might work in Unix (if the names are correct):
fid=fopen('/directory1/directory2/filename','r');

Now, the reason this does not work in Windows has nothing to do with
Matlab.
It's the way you define the file in windows.
It needs to be:
fid=fopen('../directory1/directory2/filename','r');
or
fid=fopen('c:/directory1/directory2/filename','r');
or, even better:
fid=fopen('c:\directory1\directory2\filename','r');

Subject: opening files in DIFFERENT directories

From: ImageAnalyst

Date: 5 Jul, 2009 03:26:22

Message: 3 of 3

On Jul 4, 9:14 pm, "Juliette Salexa" <juliette.physic...@gmail.com>
wrote:
> I am having problems with fopen.
>
> filename=test.m
> fopen(filename,'r')
>
> works fine, but:
>
> fopen(fullfile('directory1','directory2',filename,'r')) OR
> fopen('directory1','directory2',filename,'r') OR
> fopen(/directory1/directory2/filename,'r'))
>
> do NOT. (the latter command works in UNIX but not windows).
>
> So how do I open a file that's not in my working directory ??

----------------------------------------------------------------------------
For the first one, I don't believe fulfile() takes an "r" argument.
Move the 'r' outside the fullfile() arg list.
For the second one, you need to pass in the full filename, not a bunch
of partial filenames.
For the third one, you need to have the full filename inside single
quotes, not just hardcoded in without any quotes like you did.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
fopen Sprinceana 5 Jul, 2009 04:03:03
files in differ... Sprinceana 5 Jul, 2009 04:03:03
rssFeed for this Thread

Contact us at files@mathworks.com