Open file in appropriate application
output = open(name)
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.
Name of file or variable to open. If name does not include an extension, the open function:
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:
Open Contents.m in the MATLAB Editor by typing:
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:
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:
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.