open - Open file in appropriate application - MATLAB (original) (raw)
Open file in appropriate application
Syntax
Description
open [name](#br6vstb-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 formopen_`xxx`_
, where_xxx
_ is a file extension. For example, if you create a function openlog
, then the open
function callsopenlog
to process any files with the .log
extension. The open
function returns any single output defined by your function.
A = open([name](#br6vstb-name))
returns a structure ifname
is a MAT-file, or it returns a figure handle ifname
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
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 a file not on the MATLAB® path by including the complete file specification. If the file does not exist, MATLAB displays an error message.
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.
You have requested file: myTestFile.txt Opening in MATLAB Editor: myTestFile.txt
Input Arguments
File or variable name, specified as a character array or string scalar. Ifname
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, theopen
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). |
Open document in Adobe® Acrobat®. | |
.ppt* | Open document in Microsoft PowerPoint®. |
.xls* | Start MATLAB Import Tool. |
.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