MATLAB Function Reference    

Open files based on extension



open('name') opens the object specified by the string, name. The specific action taken upon opening depends on the type of object specified by name.

Open array name in the Array Editor (the array must be numeric)
M-file (name.m)
Open M-file name in M-file Editor
Model (name.mdl)
Open model name in Simulink
MAT-file (name.mat)
Open MAT-file and store variables in a structure in the workspace
Figure file (*.fig)
Open figure in a figure window
P-file (name.p)
Open the corresponding M-file, name.m, if it exists, in the M-file Editor
HTML file (*.html)
Open HTML document in Help browser
PDF file (*.pdf)
Open PDF document in Adobe Acrobat
Other extensions (
Open by calling the helper function openxxx, where openxxx is a user-defined function
No extension (name)
Opens name in the default editor. If name does not exist, then open checks to see if name.mdl or name.m are on the path or in the current directory and, if so, opens the file returned by which('name').

If more than one file with the specified filename, name, exists on the MATLAB path, then open opens the file returned by which('name').

If no such file name exists, then open displays an error message.

You can create your own openxxx functions to set up handlers for new file types. open('') calls the openxxx function it finds on the path. For example, create a function, openlog, if you want a handler for opening files with file extension, .log.


Example 1 - Opening a File on the Path

To open the M-file, copyfile.m, type

MATLAB opens the copyfile.m file that resides in toolbox\matlab\general. If you have a copyfile.m file in a directory that is before toolbox\matlab\general on the MATLAB path, then open opens that file instead.

Example 2 - Opening a File Not on the Path

To open a file that is not on the MATLAB path, enter the complete file specification. If no such file is found, then MATLAB displays an error message.

Example 3 - Specifying a File Without a File Extension

When you specify a file without including its file extension, MATLAB determines which file to open for you. It does this by calling which('filename').

In this example, open matrixdemos could open either an M-file or a Simulink model of the same name, since both exist on the path.

As the call, which('matrixdemos'), returns the name of the Simulink model, open opens the matrixdemos model rather than the M-file of that name.

Example 4 - Opening a MAT File

This example opens a MAT-file containing MATLAB data and then keeps just one of the variables from that file. The others are overwritten when ans is reused by MATLAB.

Example 5 - Using a User-Defined Handler Function

If you create an M-file function called opencht to handle files with extension .cht, and then issue the command

open will call your handler function with the following syntax.

See Also

load, save, saveas, which, file_formats, path

  ones openfig