File path in MATLAB

Find this useful? Support us: Star on GitHub 6
Category: File | Language: MATLAB

In MATLAB, we can obtain the file path by using the which and pwd functions.

The which function returns the full path of a specified file:

path = which('filename.ext')

For example, if we want to get the file path of a file named "mydata.txt", we can use:

path = which('mydata.txt')

The pwd function returns the current working directory path:

path = pwd

For example, if our current working directory is "C:\Users\Username\Documents", then pwd will return:

path = 'C:\Users\Username\Documents'

We can combine these functions to get the absolute path of a file in the current working directory:

filepath = fullfile(pwd, 'filename.ext')

For example:

filepath = fullfile(pwd, 'mydata.txt')

This will return the file path as a string:

filepath = 'C:\Users\Username\Documents\mydata.txt'