Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

open

Open file in appropriate application

Syntax

Description

example

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

A = open(name) returns a structure if name is a MAT-file or 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 before toolbox\matlab\strfun on the MATLAB path, 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.

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 a text file.

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

Name of file or variable to open, specified as a character array. If name does not include an extension, 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 structureA 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.

See Also

| | | | |

Introduced before R2006a

Was this topic helpful?