John Manak
John Manak

Reputation: 13558

Matlab doesn't read files with tilde (~) instead of full home path

I've noticed a strange behavior of Matlab when accessing files. Say I have a path to a file like this:

path = '~/data/file'

If I run exist(path), the result is 2, i.e. the file exists. If I run

textread(path, '%s')

then I get an error message

Error using dataread
File not found or permission denied.

However, if I expand the tilde and run textread, it works fine:

path2 = '/home/username/data/file'
textread(path2, '%s')

Can you explain this behavior?

Upvotes: 3

Views: 1679

Answers (1)

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

Yes, exist understands relative paths (as identified by the tilde), whereas textread does not.

Note that textscan is now the preferred way of reading data from a file - this accepts file identifiers so will work with relative paths.

If this is not an option, a good GetFullPath function can be found here.

Upvotes: 3

Related Questions