Programming and Data Types    

Files and Filenames

This section covers the following topics:

Naming M-files

M-file names must start with an alphabetic character, may contain any alphanumeric characters or underscores, and must be no longer than the maximum allowed M-file name length (returned by the function namelengthmax).

Since variables must obey similar rules, you can use the isvarname function to check whether a filename (minus its .m file extension) is valid for an M-file.

Naming Other Files

The names of other files that MATLAB interacts with (e.g., MAT, MEX, and MDL-files) follow the same rules as M-files, but may be of any length.

Depending on your operating system, you may be able to include certain non-alphanumeric characters in your filenames. Check your operating system manual for information on valid filename restrictions.

Passing Filenames as Arguments

In MATLAB commands, you can specify a filename argument using the MATLAB command or function syntax. For example, either of the following are acceptable. (The .mat file extension is optional for save and load).

If you assign the output to a variable, you must use the function syntax.

Passing Filenames to ASCII Files

ASCII files are specified as follows. Here, the file extension is required.

Determining Filenames at Runtime

There are several ways that your function code can work on specific files without you having to hard-code their filenames into the program. You can

For more information: See Obtaining User Input in the MATLAB "Programming and Data Types" documentation, and the input and uigetfile function reference pages

Returning the Size of a File

Two ways to have your program determine the size of a file are shown here.

The dir function also returns the filename (, last modification date (, and whether or not it's a directory (s.isdir).

(The second method requires read access to the file.)

For more information: See the fopen, fseek, ftell, and fclose function reference pages

  Save and Load Input/Output