open

Open file in appropriate application

Syntax

open(name)
output = open(name)

Description

open(name) opens the specified file or variable in the appropriate application.

output = open(name) returns an empty output ([]) for most cases. If opening a MAT-file, output is a structure that contains the variables in the file. If opening a figure, output is a handle to that figure.

Input Arguments

name

Name of file or variable to open. If name does not include an extension, the open function:

  1. Searches for a variable named name. If the variable exists, open opens it in the Variables editor.

  2. Searches the MATLAB® path for name.mdl, name.slx, or name.m. If name.mdl or name.slx exists, then open opens the model in Simulink®. If only name.m exists, open opens the file in the MATLAB Editor.

If more than one file named name exists on the MATLAB path, the open function opens the file returned by which(name).

The open function performs the following actions based on the file extension:

.m

Open in MATLAB Editor.

.mat

Return variables in structure st when called with the syntax:

st = open(name)

.fig

Open figure in Handle Graphics®.

.mdl or .slx

Open model in Simulink.

.prj

Open project in the MATLAB Compiler Deployment Tool.

.doc*

Open document in Microsoft® Word.

.exe

Run executable file (only on Windows® systems).

.pdf

Open document in Adobe® Acrobat®.

.ppt*

Open document in Microsoft PowerPoint®.

.xls*

Start MATLAB Import Wizard.

.htm or .html

Open document in MATLAB browser.

.url

Open file in your default Web browser.

Examples

Open Contents.m in the MATLAB Editor by typing:

open Contents.m

Generally, MATLAB opens matlabroot\toolbox\matlab\general\Contents.m. However, if you have a file called Contents.m in a directory that is before toolbox\matlab\general on the MATLAB path, then open opens that file instead.

Open a file not on the MATLAB path by including the complete file specification:

open('D:\temp\data.mat')

If the file does not exist, MATLAB displays an error message.

Create a function called opentxt to handle files with extension .txt:

function opentxt(filename)

   fprintf('You have requested file: %s\n', filename);

   wh = which(filename);
   if exist(filename, 'file') == 2
     fprintf('Opening in MATLAB Editor: %s\n', filename);
     edit(filename);
   elseif ~isempty(wh)
     fprintf('Opening in MATLAB Editor: %s\n', wh);
     edit(wh);
   else
     warning('MATLAB:fileNotFound', ...
             'File was not found: %s', filename);
   end

end  

Open the file ngc6543a.txt (a description of ngc6543a.jpg, located in matlabroot\toolbox\matlab\demos):

photo_text = 'ngc6543a.txt';
open(photo_text)

open calls your function with the following syntax:

opentxt(photo_text)

More About

expand all

Tips

The open function opens files based on their extension. You can extend the functionality of open by defining your own file handling function of the form openxxx, where xxx is a file extension. For example, if you create a function openlog, the open function calls openlog to process any files with the .log extension. The open function returns any single output defined by your function.

Was this topic helpful?