Can someone better explain what the fullfile function does on Matlab (the mathworks explanation didn't help me)?

369 views (last 30 days)
I read the explanation on mathworks (below), but still do not understand what the fullfile function does. Can someone explain is more clearly to someone who is new to matlab? For example, I'm not really sure what a 'full file specification' is or what 'platform-dependent' means. Thank you!
f = fullfile(filepart1,...,filepartN) builds a full file specification from the specified folder and file names. fullfile inserts platform-dependent file separators where necessary, but does not add a trailing file separator. On Windows® platforms, the file separator character is a backslash (\). On other platforms, the file separator might be a different character.
fullfile replaces all forward slashes (/) with backslashes (\) on Windows. On UNIX® platforms, the backlash (\) character is a valid character in file names and is not replaced.
fullfile does not trim leading or trailing separators. fullfile collapses inner repeated file separators unless they appear at the beginning of the full file specification. fullfile also collapses relative directories indicated by the dot symbol, unless they appear at the end of the full file specification. Relative directories indicated by the double-dot symbol are not collapsed.

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 5 Jun 2020
If you want to create a filename that contains a full or relative path to the directory where the file resides you can use a construction like:
root = '/';
dirA = 'data_dir';
dirB = 'cameraB';
dirDate = '20200605';
filename = 'img1.png';
filename = [root,dirA,'/',dirB,'/',dirDate,'/',filename];
which would generate a perfectly valid full unix-filename. If you write code like that and get to share it with someone working in a MS-windows system problems might appear - since the file-separator is different. To avoid that you use fullfile to automatically handle the OS-specifics.
root = '/'; % you'd still have to handle the "drive" differently MS-OS
dirA = 'data_dir';
dirB = 'cameraB';
dirDate = '20200605';
filename = 'img1.png';
filename = fullfile(root,dirA,dirB,dirDate,filename);
would generate a filename:
filename = '/data_dir/cameraB/20200605/img1.png'
in unix-like OSes and something like this in MS-OS:
filename = 'C:\data_dir\cameraB\20200605\img1.png'
if you handled the root-part right.
So use for transportability of your code - is more important than you might think.
HTH

Star Strider
Star Strider on 5 Jun 2020
You don’t have to actually write any files to experiment with it, so create a directory path and a filename and extension (they can be anytyhing you want — within limits — and don't necessarily have to be anything currently on your computer) and experiment with it to see what it produces.
That’s always the easiest way to find out what a MATLAB function such as fullfile and fileparts (a related function it would be good to understand) do.

Categories

Find more on File Name Construction in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!