Main Content

open

在合适的应用程序中打开文件

说明

示例

open name 在适当的应用程序中打开指定的文件或变量。

您可以通过 openxxx 的形式(其中 xxx 为文件扩展名)定义您自己的文件处理函数,以此扩展 open 的功能。例如,如果您创建了函数 openlog,则 open 函数将调用 openlog 来处理带有 .log 扩展名的任何文件。open 函数将返回您的函数所定义的任何单个输出。

如果 name 是 MAT 文件,A = open(name) 将返回结构体;如果 name 是图窗,则返回图窗句柄。否则,open 将返回空数组。要获得更高的灵活性和更多选项,请使用 load 函数打开 MAT 文件,使用 openfig 函数打开图窗。

示例

全部折叠

在编辑器中打开文件 num2str.m。MATLAB 会打开文件 matlabroot\toolbox\matlab\strfun\num2str.m。但是,如果在 MATLAB 路径中 toolbox\matlab\strfun 之上的文件夹中存在名为 num2str.m 的文件,MATLAB 将打开该文件。

open num2str.m

通过指定文件的完整路径,可打开不在 MATLAB® 路径中的文件。如果文件不存在,MATLAB 将显示错误消息。

open C:\temp\data.mat

创建名为 opentxt 的函数,以处理具有 .txt 扩展名的文件。

创建函数 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

创建文本文件 myTestFile.txt

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

读取该文件中的数据。open 函数将调用函数 opentxt 来打开该文件。

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

输入参数

全部折叠

文件或变量的名称,指定为字符向量或字符串标量。如果 name 不包含扩展名,MATLAB 将根据函数优先顺序来搜索变量和文件。如果 name 是变量,open 函数将在变量编辑器中打开它。否则,open 函数将根据文件扩展名执行以下操作之一。

.m.mlx

在 MATLAB 编辑器中打开代码文件。

.mat

使用语法 A = open(name) 调用时,返回结构体 A 中的变量。

.fig

在图窗窗口中打开图窗。

.mdl.slx

在 Simulink® 中打开模型。

.prj

在 MATLAB Compiler 部署工具中打开工程。

.doc*

Microsoft® Word 中打开文档。

.exe

运行可执行文件(仅在 Windows® 系统上)。

.pdf

Adobe® Acrobat® 中打开文档。

.ppt*

Microsoft PowerPoint® 中打开文档。

.xls*

启动 MATLAB 导入向导。

.htm.html

在 MATLAB 浏览器中打开文档。

.slxc

打开 Simulink 缓存文件的报告文件。

MATLAB Online™ 中,open 仅支持打开 MAT 文件、图窗、代码文件(.m.mlx)和 HTML 文档。

数据类型: char | string

版本历史记录

在 R2006a 之前推出

另请参阅

| | | | |