Main Content

open

Open file in appropriate application

Description

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

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, then the open function calls openlog to process any files with the .log extension. The open function returns any single output defined by your function.

example

A = open(name) returns a structure if name is a MAT-file, or it returns a figure handle if name is a figure. Otherwise, open returns an empty array. For increased flexibility and options, use the load function to open MAT-files, and the openfig function to open figures.

Examples

collapse all

Open the file num2str.m in the Editor. MATLAB opens the file matlabroot\toolbox\matlab\strfun\num2str.m. If a file called num2str.m exists in a folder that is above toolbox\matlab\strfun on the MATLAB path, then MATLAB opens that file instead.

open num2str.m

Open a file not on the MATLAB® path by including the complete file specification. If the file does not exist, MATLAB displays an error message.

open C:\temp\data.mat

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

Create the function opentxt.

function opentxt(filename)
   [~, name, ext] = fileparts(filename); 
   fprintf('You have requested file: %s\n', [name ext]);

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

Create the text file myTestFile.txt.

dlmwrite('myTestFile.txt',[1,2,3,4]);

Read the data from the file. The open function calls the function opentxt to open the file.

open('myTestFile.txt');
You have requested file: myTestFile.txt
Opening in MATLAB Editor: myTestFile.txt

Input Arguments

collapse all

File or variable name, specified as a character array or string scalar. If name does not include an extension, then MATLAB searches for variables and files according to the Function Precedence Order. If name is a variable, the open function opens it in the Variables editor. Otherwise, the open function performs one of these actions based on the file extension.

.m or .mlx

Open code file in MATLAB Editor.

.mat

Return variables in structure A when called with the syntax A = open(name).

.fig

Open figure in Figure window.

.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.

.slxc

Open report file for the Simulink cache file.

In MATLAB Online™, open only supports opening MAT-files, figures, code files (.m or .mlx), and HTML documents.

Data Types: char | string

Version History

Introduced before R2006a

See Also

| | | | |