File Management Operations

About MATLAB® File Operations

MATLAB® file operations use the current directory and the MATLAB search path as reference points. Any file you want to run must either be in the current directory or on the search path. The key tools for performing file operations are the current directory field and the Current Directory browser.

Current Directory Field

A quick way to view or change the current directory is by using the current directory field in the desktop toolbar.

Image of current directory field.

To change the current directory from this field, do one of the following:

The current directory field in the desktop also appears in the Current Directory browser, when the Current Directory browser is undocked. Consider it to be one tool with two different means of accessing it.

Current Directory Browser

To search for, view, open, find, and make changes to directories and files related to MATLAB, use the Current Directory browser. Most features of the Current Directory browser have equivalent functions that perform similar actions. If you have an active Internet connection, you can watch the Current Directory Browser video demo for an overview of the major features.

In addition to the features described here, the Current Directory browser includes tools to help you manage your M-files—see Directory Reports in Current Directory Browser.

To open the Current Directory browser, select Desktop > Current Directory from the MATLAB desktop, or type filebrowser at the Command Window prompt. The Current Directory browser opens.

Image of Current Directory browser showing key features.

The main tasks you perform with the Current Directory browser are:

Viewing and Making Changes to Directories

You can change the current working directory, view its contents, add directories to the MATLAB search path, and change the way the Current Directory browser presents entries.

Changing the Current Working Directory and Viewing Its Contents

To change the current directory, use the current directory field. The Current Directory browser lists the files and directories in the current directory.

To view the contents of a subdirectory, double-click it, or select the subdirectory and press Enter or Return.

To move up one level in the directory structure, click the Go Up One Level button , or press the backspace (<-) key.

You can specify the current directory for MATLAB upon startup, called the startup directory—for more information, see Startup Directory for the MATLAB® Program.

Function Alternative.   To view the contents of the current working directory or another specified directory, use dir. Use a return argument with dir to get a structure containing the names of the files in the directory, their last modified date and time, and so on.

Use what with no arguments to display files related to MATLAB that are in the current working directory. Use which to display the path for a specified function. Use exist to see if a directory or file exists. Use fileattrib to see or set file attributes, much like attrib on DOS or chmod on UNIX® platforms.

Searching the Current Directory Browser

You can search the Current Directory browser for files and directories by typing directly in the window. As you type, the Current Directory browser searches downward from the top of the window to find an entry that matches what you have typed. For example:

  1. Set C:\Climate as your current directory.

  2. Assume that you want to search the Current Directory for the file named standard.m. Begin your search by positioning the cursor anywhere within the Current Directory browser.

  3. Type the letter s. The Current Directory browser searches to find the first entry beginning with the letter s. In this example it stops at the directory named standalone. Note as you begin typing that a small yellow-background Search filenames for: dialog box appears at the top of the Current Directory browser window. This dialog box keeps track of your search target as you type additional letters to narrow the focus of your search.

  4. Because the standalone directory is not your intended search target, continue typing additional letters that identify your search target, eventually entering the letters standar.

  5. When you have entered the letters standar, the search resumes, stopping this time at the standard.m file, your intended search target.

Changing the Display

Types of Files.   To specify the types of files shown in the Current Directory browser, use the View menu. For example, you can show only M-files. If All Files is selected and you want to see specific file types, first clear the selection for All Files and then select the specific file types.

Columns.   To show or hide columns, use preferences for the Current Directory browser. Select File > Preferences > Current Directory and select or clear the check boxes for Browser display options. For more information, see Browser Display Options.

You can sort the information shown in the Current Directory browser by column. Click the title of the column on which you want to sort. The display is sorted, with the information in that column shown in ascending order, and an up arrow indicating the direction. Click a second time on the column title to sort the information in descending order.

Contents.   In the Current Directory browser, select a file and then view information about the file in the Current Directory browser's lower pane. To do this, you must first select File > Preferences > Current Directory and under Browser display options, select the check box Show M, MDL and MAT file contents.

For an M-file, it shows the M-file help. For a Simulink® model, it shows the complete description, allowing you to view information about a model without having to start the Simulink software. For a MAT-file, it displays the names of its variables along with their size, bytes, and class, allowing you to view the content of a MAT-file without loading it.

You can view more extensive help for the M-file selected in the Current Directory browser. From the context menu, select View Help. The reference page for that function appears in the Help browser.

Adding Directories to the MATLAB® Search Path

From the Current Directory browser, you can add directories to the MATLAB search path. Right-click and from the context menu, select Add to Path. Then select one of these options:

Creating, Renaming, Copying, and Removing Directories and Files

General Notes

If you have write permission, you can create, copy, remove, and rename files and directories related to MATLAB for the directory shown in the Current Directory browser. If you do not have write permission, you can still copy files and directories to another directory, or you can use equivalent functions, such as movefile.

To run functions whose arguments require the use of a path name or file name, use the function form rather than the unquoted or command form of the syntax when the path name or file name includes spaces. For example, the command form

delete my file.m

generates a warning and does not delete my file.m. Instead use the function form of the syntax:

delete('my file.m')

Creating New M-Files

To create a new file using the Current Directory browser:

  1. Right-click, and then from the context menu, select New, and then select one of the following:

    A new M-file named Untitledn appears at the end of the list of files shown in the Current Directory browser. An M-file icon, , displays next to the new file.

  2. Type over Untitledn with the name you want to give to the new M-file.

  3. Press Enter or Return.

    MATLAB creates the new M-file.

  4. To enter or edit the contents of the new M-file, open it—see Opening and Running Files.

Function Alternative.   To create a new M-file or other type of text file in the Editor, use the edit function.

Creating New Directories

To create a new directory using the Current Directory browser:

  1. Click the New Folder button in the Current Directory browser toolbar, or right-click and then select New > Folder from the context menu.

    A folder icon, with the default name NewFoldern appears at the end of the list of files shown in the Current Directory browser.

  2. Type over NewFoldern with the name you want to give to the new directory.

  3. Press the Enter or Return key.

    The directory is added.

Function Alternative.   To create a directory, use the mkdir function. For example,

mkdir newdir

creates the directory newdir within the current directory.

Renaming Files and Directories

To rename a file or directory:

  1. Select the item that you want to rename.

  2. Right-click, and select Rename from the context menu.

  3. Type over the existing name with the new name for the file or directory, and press Enter or Return.

The file or directory is renamed.

Function Alternative.   To rename a file or directory, use movefile. For example,

movefile('myfile.m','projectresults.m')

renames myfile.m to projectresults.m.

Cutting or Deleting Files and Directories

To cut or delete files and directories:

  1. Select the files and directories to remove. Use Shift+click or Ctrl+click to select multiple items.

  2. Right-click and select Cut or Delete from the context menu.

    The files and directories are removed.

Files and directories that you delete from the Current Directory browser go to the Recycle Bin on Microsoft® Windows® platforms (or the Trash Can on Macintosh® platforms). If you do not want the selected items to go to the Recycle Bin, press Shift+Delete. A confirmation dialog box displays before the items are deleted, assuming you have set that option in your operating system. For example, on Windows platforms, right-click the Recycle Bin, select Properties from the context menu, and then, under the Global tab, select the check box to Display delete confirmation dialog.

Function Alternative.   To delete a file, use the delete function. For example,

delete('d:/mymfiles/testfun.m')

deletes the file testfun.m. You can recover deleted files if you use the recycle function or the equivalent preference described in Default Behavior of the Delete Function.

To delete a directory and optionally its contents, use rmdir. For example,

rmdir('myfiles')

removes the directory myfiles from the current directory.

Copying and Pasting Files and Directories

Use the Current Directory browser, to copy (or cut) and paste files and directories:

  1. Select the files or directories to copy. Use Shift+click or Ctrl+click to select multiple items. For a directory, the entire contents are copied, including all subdirectories and files.

  2. Right-click and select Copy from the context menu.

  3. Navigate to the file or directory where you want to paste the items you just copied.

  4. Right-click and select Paste from the context menu.

You can also copy and paste files and directories to and from tools outside of MATLAB, such as Windows Explorer. You can use Current Directory browser menu items, or keyboard shortcuts, or you can drag the items.

Function Alternative.   To cut and paste or to copy and paste files or directories, use movefile or copyfile. For example, to make a copy of the file myfun.m in the current directory, assigning it the name myfun2.m, type

copyfile('myfun.m','myfun2.m')

Opening and Running Files

Opening Files

You can open a file from the Current Directory browser and the file opens in the tool associated with that file type.

To open a file, select one or more files and perform one of the following actions:

The file opens in the appropriate tool, provided that the tool is installed on your system. For example, the Editor opens for M-files, and Simulink software opens for model (.mdl) files.

To open any file in the Editor, except P-files (.p), select Open as Text from the context menu. You cannot open P-files.

To open a file using an external application, select Open Outside MATLAB from the context menu. For example, if you right–click myfile.doc, and select Open Outside MATLAB, then myfile.doc opens in Microsoft Word (assuming you have the .doc file association configured to start Microsoft Word). This is useful for file types associated with MATLAB software that are also associated with an external application on the Windows platform. For example, .mat is the extension for MATLAB data files as well as Microsoft® Access™ files. When you double-click a .mat file in the Current Directory browser, it loads the MATLAB data file into the workspace. If instead you want to open the .mat file in Microsoft Access, right-click it and select Open Outside MATLAB from the context menu. MATLAB opens the file using the application you associated with that file type on the Windows platform. For more information, see Changing File Associations for the MATLAB® Program from the Windows® Environment.

You can also import data from a file. Select the file, right-click, and select Import Data from the context menu. The Import Wizard opens. See the Import Wizard documentation for instructions on importing the data.

You can run a Windows shortcut directly from the Current Directory browser. Double-click the shortcut icon in the Current Directory browser to perform the Windows operation.

Function Alternative.   Use the open function to open a file in the tool appropriate for the file, given its file extension. Default behavior is provided for standard MATLAB file types. You can add other file types and override the default behavior for the standard files. For name.ext, open performs the following actions.

File Type

Extension

Action

Figure file

fig

Opens figure name.fig in a figure window.

HTML file

html

Opens HTML file name.html in the MATLAB Web browser.

M-file

m

Opens M-file name.m in the Editor.

MAT-file

mat

Opens MAT-file name.mat in the Import Wizard.

Model

mdl

Opens model name.mdl in the Simulink.

PDF file

pdf

Opens the PDF file name.pdf in the installed PDF reader, for example, Adobe® Acrobat®.

Variable

none

Opens the numeric or string array name in the Variable Editor; open calls openvar.

Other

custom

Opens name.custom by calling the helper function opencustom, where opencustom is a user-defined function.

Use winopen to open a file using an external application on Windows platforms.

To view the content of an ASCII file, such as an M-file, use the type function. For example

type('startup')

displays the contents of the file startup.m in the Command Window.

Running M-Files

To run an M-file from the Current Directory browser, select it, right-click, and select Run from the context menu. The results appear in the Command Window.

Finding Files and Content Within Files

Use the Find Files tool to search for files or for specified text within files.

Image of Find Files dialog box showing key features. panel of results.

To search for files in one or more directories, or to search for specified text in files, follow these instructions:

  1. Open the Find Files tool by clicking the Find Files button in the Current Directory browser toolbar, or by selecting Edit > Find Files from any desktop tool, such as the Current Directory browser or the Editor.

    The Find Files dialog box opens.

  2. Type the file name and/or text you are searching for:

    Click the down arrow next to each field to select previous entries from the current MATLAB session.

  3. You can restrict the types of files to search by selecting an option in Include only file type(s). For example, select *.m to limit the search to M-files only.

    With All files (*) selected, use Skip file types (under More options) to ignore files of the specified type. For details, see Skip File Types in Find Files.

  4. From the Look in list box, select the directories to search in. Select the MATLAB current directory or MATLAB search path, or use the Browse option to select another directory. You can instead type the full path for one or more directories into this field, with each path separated by a semicolon (;). To include subdirectories in the search, select the Include subdirectories check box.

  5. Use additional entries under More options to further restrict the search:

  6. To execute the search, click Find. While the search is in progress, the Find button label changes to Stop Find. To abort a search, click Stop Find.

    Search results appear in the pane on the right side of the Find Files dialog box, with a summary of the results at the bottom of the pane. For text searches, the line number and line of code are shown. To see the full path names for the files, select the Show full pathnames check box.

  7. Click a column heading to sort the results based on that column. Click the column heading again to reverse the sort order for that column. For example, click Line to sort results by line number.

Opening Files from Find Files

To open files shown in the results list, do one of the following:

The files open in the Editor. For text searches, the file opens scrolled to the line number shown in the results section of the Find Files dialog box. Once in the Editor, you can use the Find & Replace tool to change specified text.

Previous Results of Find Files

To see the results of a previous search, select its tab at the bottom of the results pane. Find Files shows up 10 search result tabs while the tool is open, but does not maintain the results after you close the tool.

MATLAB software maintains the state for options in the Find Files tool even after you end the session.

Skip File Types in Find Files

In the Find Files tool, you can restrict the search to look in all file types except those you specify:

  1. For Include only file type(s), select All files (*).

  2. Select the Skip file type(s) check box.

  3. Click Edit to view or change the list of file types the search ignores.

    The Edit Skipped File Extensions dialog box opens.

    Image of Edit Skipped File Extensions dialog box.

  4. Find Files will not look in any file type in the list whose State check box is selected. It will look in any file type in the list whose State check box is cleared.

    1. Clear or select the State check box as needed to instruct Find Files about file types to skip.

    2. If you want Find Files to skip a file type not shown in the list, enter the file extension in the field at the top of the dialog box and click Add. The type appears in the list. Be sure its State check box is selected. For the example shown, the scc file type was added.

    3. You can reduce the size of the list by removing any file extensions. Select the name of the extension and click Remove.

  5. Click OK to accept the changes and close the Edit Skipped File Extensions dialog box.

  6. When you click Find in the Find Files tool, the search ignores the selected file types.

Function Alternative

Use lookfor to search for the specified text in the first line of help for all M-files on the search path.

Comparing Files and Directories

The MATLAB File and Directory Comparisons tool determines and displays the differences between two files or two directories.

For information on using the tool, its results, and other options, see Comparing Files and Directories.

Accessing Source Control Features

Select a file or files in the Current Directory browser and right-click to view the context menu. From there you can access features for source control. For details on these features, see Source Control Interface.

Preferences for the Current Directory Browser

Using preferences, you can specify the number of recently used current directories to maintain in the history list as well as the type of information to display in the Current Directory browser.

From the Current Directory browser, select File > Preferences. The Current Directory Preferences pane appears in the Preferences dialog box.

Image of Preferences dialog box showing the Current Directory browser preferences.

History

The drop-down list in the current directory field shows the history of current directories, that is, the most recently used current directories.

Saving Directories.   When the MATLAB session ends, the list of directories will be maintained. Use the Save most recent directories field to specify how many directories will appear on the list at the start of the next MATLAB session.

Removing Directories.   To remove all entries in the list, click Clear History. The list is cleared immediately.

Browser Display Options

In the Current Directory browser, you can view or hide the following information by selecting the appropriate Browser display options:

For more information, see Changing the Display.

Auto-Refresh

By default, the Auto-refresh directory view check box is selected, with an update time of 2 seconds. This means that every 2 seconds, the Current Directory browser checks for and reflects any changes you made to files and directories in the current directory using other applications.

In some cases when the current directory is on a network, MATLAB becomes slow because of the auto-refresh feature in the Current Directory browser. If you experience general slowness in MATLAB and have the Current Directory browser open, try increasing the default update time to alleviate this problem. For extremely slow performance situations, clear the check box to turn auto-refresh off. You can then right-click and select Refresh from the context menu to update the Current Directory browser display.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS