Skip to Main Content Skip to Search
Product Documentation

Creating, Opening, Changing, and Deleting Files and Folders

Creating New Files and Folders

You can add files and subfolders to your current folder with the Current Folder browser or by typing commands.

Creating Files and Folders with the Current Folder Browser

  1. In the Current Folder browser, navigate to the folder where you want to create a file or folder.

    For guidance on where to create files, see Locations for Storing Your Files.

  2. Right-click in white space, and then select one of the following from the context menu:

    • New Folder.

      MATLAB creates and selects a folder named New Folder.

    • New File > file-type,

      For file-type you can choose: Script, Function, Class, Enumeration, Model (if Simulink is installed), or Zip File. Function, class, and enumeration files that you create this way contain template information representing the fundamental elements for the file (such as function arguments).

    MATLAB creates and selects a new file named untitled with the appropriate extension.

  3. Replace the selected name by typing a new name.

    For file naming conventions, see Function Name and Naming Functions.

  4. Press Enter.

Creating and Updating MAT-Files with the Current Folder Browser

To create or update a MAT-file using variables in the workspace:

  1. In the Current Folder browser, change the current folder to the folder where you want to save the variables. See Locations for Storing Your Files.

  2. In the Workspace browser, select a variable to save. Hold down the Ctrl key and click any other variable names you want to include in the MAT-file.

  3. Drag the selected variables from the Workspace browser to the Current Folder browser.

  4. Drop the variables in the Current Folder browser:

    • Create a MAT-file by dropping the variables onto any empty location in the Current Folder browser. Then name the file.

    • Update an existing MAT-file by dropping the variables onto the file name.

      MATLAB warns you when the MAT-file contains variables of the same name. To update the existing variables, click Continue. Otherwise, click Cancel.

      To suppress the warning, select File > Preferences > General > Confirmation Dialogs, and clear the preference, Confirm when overwriting variables in MAT-files.

See also Opening Files and Importing Data Using the Current Folder Browser.

Creating and Managing Zip File Archives

To back up files, conserve file storage space, or to forward collections of files to other people, create archives using zip files. You can create, view, and adjust the contents of a zip file from within the Current Folder browser, as described in the sections that follow.

Viewing the Contents of Zip Files.  To view the contents of a zip file without extracting any files it contains, click the associated + (expand) button in the Current Folder browser. This feature is helpful when you want to:

The following image shows graphics.zip expanded within the Current Folder browser. By default, files within a zip file appear dimmed to indicate that they are not on the MATLAB path.

Creating Zip Archives.  You can either create an empty archive, or select files, folders, or both to create an initial archive. In either case, you can add more files later.

  1. Do one of the following in the Current Folder browser:

    • Create an empty zip file:

      Right-click white space, and then select New File > Zip File.

    • Create a populated zip file from selected files, folders, or both:

      Select the folders and files you want to archive, right-click, and then select Create Zip File.

    In either case, MATLAB creates an archive with a default name of Untitledn.zip, where n is an integer.

  2. Type over the default file name to specify a descriptive name, for example listmaster_export.zip, as shown here.

See also Adding Files to a Zip Archive.

Extracting Files from Zip Files.  To extract a single file from within a zip file in the Current Folder browser, do one of the following:

MATLAB extracts the file and saves it to the folder where you dragged or pasted it.

To extract all the files from a zip file, do one of the following:

MATLAB extracts the entire contents of the zip file into a folder having the same name as the zip file, as shown here.

Because MATLAB creates a folder when extracting files, none of the extracted files can overwrite files that have the same name. If you attempt to overwrite a folder with the same name when extracting, MATLAB prompts you to determine what you want to do.

Adding Files to a Zip Archive.  To add files and folders to a zip file archive in the Current Folder browser, do one of the following:

If the archive contains a file or folder with the same name as the one you are adding, a MATLAB dialog box opens. The dialog box asks if you want to replace the existing file in the archive.

Comparing the Contents of a Zip Archive to Unzipped Files and Folders.  To determine differences between archived and unarchived files, use the Comparison Tool from within the Current Folder browser as you would for any other files and folders.

For instance:

For details, see Comparing Files and Folders.

Creating Files and Folders Using Functions

As an alternative to using the Current Folder browser to create files and folders, you can run functions in the Command Window or from a script.

To...Use This Function
Create a foldermkdir
Create a text file, such as a MATLAB program fileedit
Create a MAT-filesave
Create archive of fileszip, gzip, tar
Extract files from archiveunzip, gunzip, untar

See also Locations for Storing Your Files.

Copying, Renaming, and Deleting Files and Folders

Renaming Files Using the Current Folder Browser

  1. Select the item to rename.

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

  3. Type over the existing name with the new name. Warnings appear when:

  4. Press Enter.

Renaming Files and Folders Using Functions

Use the movefile function.

Deleting Files and Folders Using the Current Folder Browser

To remove items:

  1. Select the item to delete. To select multiple items:

    • Click to the left of an icon and drag up or down to select contiguous items

    • Shift+click to select contiguous items

    • Ctrl+click to select non-contiguous items

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

When you delete a file or folder using the Current Folder browser, MATLAB permanently removes it or moves it to another location, based on your platform.

PlatformBehavior Deleting Files and Folders Using the Current Folder Browser
MicrosoftWindows platforms

Follows the Windows system preference for sending files to the Recycle Bin. Some systems only allow recycling of local files and not files accessed on a network.

To delete a selection permanently when the system preference is set to recycle, press Shift+Delete.

Linux platformsSpecify the behavior:
  1. Select File > Preferences > General.

  2. Set the Deleting files option you want.

To move files to a temporary folder, determine the location by running tempdir.

To delete a selection permanently when the preference is set to recycle, press Shift+Delete.

Apple Macintosh platformsFollows your Macintosh system preference for sending files to the Trash.

Deleting Files and Folders Using Functions

To...Use This Function
Delete a filedelete
Delete a folderrmdir

You cannot recover folders deleted using rmdir.

By default, the delete function permanently deletes files. To move them to a different location instead, use the Deleting files preference:

  1. From any desktop tool, select File > Preferences > General.

  2. Set the Deleting files option you want.

Setting the preference to delete files permanently makes delete run faster.

To override the preference when using the delete function, use the recycle function.

The location for deleted files varies by platform, as the following table indicates.

PlatformLocation for Files Not Permanently Deleted Using the delete Function
MicrosoftWindows platformsRecycle Bin. Some systems only allow recycling of local files and not files accessed on a network.
Linux platforms

MATLAB_Files_<day>-<mo>-<yr>_<hr>_<min>_<sec> folder in the location returned by the tempdir function.

For example, when tempdir returns /tmp, files deleted at 2:09:28 in the afternoon of November 9, 2009 move to /tmp/MATLAB_Files_09-Nov-2009_14_09_28.

Apple Macintosh platformsTrash

Deleted files remain in these locations until you remove them. To remove deleted files, use operating system features, such as Empty Recycle Bin on Windows platforms.

Copying and Moving Files and Folders

Copy and move files and folders using the Current Folder browser using standard GUI practices. For example, click and drag a file from one folder to another or to another application, such as Windows Explorer.

To copy and move files and folders using functions, use copyfile and movefile.

Changing Properties of Files and Folders

To change some properties of files and folders, such as read/write permissions, use the fileattrib function.

Opening and Running Files

Opening Files and Importing Data Using the Current Folder Browser

  1. In the Current Folder browser, right-click the file you want to open or load.

  2. From the context menu, select an option for opening or importing the file:

    • Open — Opens the file using the appropriate MATLAB tool for the file type. For example, this option loads a MAT-file into the Workspace browser.

    • Open in GUIDE — Opens a FIG-file in GUIDE instead of a figure window.

      For more information, see Opening GUIDE.

    • Open as Text — Opens the file in the Editor as a text file, even if the file type is associated with another application or tool.

      This is useful, for example, if you have imported a tab-delimited data file (.dat) into the workspace and you find you want to add a data point. Open the file as text in the Editor, make your addition, and then save the file.

    • Open Outside MATLAB — Opens the file using the application or tool that the operating system associates with the file type.

      For example, .mat is the extension for MATLAB data files and Microsoft Access files. Whereas Open loads the file into the MATLAB workspace, Open Outside MATLAB opens the file into Microsoft Access. See Managing File Associations for MATLAB on Windows Systems.

For information on how to view information about a file without opening it, see Viewing File Details Without Opening Files.

Opening Files and Importing Data Using Functions

To...Use This Function
Open a file or open a variable in the Variable Editoropen
Add variables from a MAT-file to the workspaceload
Import data filesimportdata
Import data file using the Import Wizarduiimport
Access the system clipboardclipboard

See Also.  

Running MATLAB Program Files from the Current Folder Browser

For convenience, you can run MATLAB scripts and functions from the Current Folder browser. Script files do not accept input arguments or return values and can be run directly. If the program is a function which requires input arguments or returns output arguments, you can define a run configuration for it that defines arguments. See Using Run Configurations for Functions. Run any program file in the following way:

  1. In the Current Folder browser, change the current folder to the folder containing the file to run.

  2. Right-click the file name to open the context menu.

  3. (Optional) If you have defined a run configuration for the file you want to use, select it from the Run Configurations on the context menu. Select Edit Configurations to edit or create one.

  4. From the context menu, select Run.

If you have customized the Current Folder Browser toolbar with a Run button , you can select the file and then click the Run button. That button has a dropdown list of function run configurations you have defined. For details, see Toolbar Customization.

Using Run Configurations for Functions.   If you run a function that requires input arguments, executing it from the Run context menu or toolbar button item might not work properly. Specify default input arguments for functions that require them by defining run configurations for them. To create a run configuration:

  1. Right-click the name of a function in the Current Folder browser and select Run Configurations > Edit Configurations.

  2. Give your run configuration a name.

  3. Type in the expressions for running the function in the MATLAB expression panel.

  4. Click Run if you want to test the configuration.

  5. Click Close to save the configuration and exit the dialog box.

Executing a function with a run configuration sets up function arguments as the configuration specifies. You can create multiple configurations for a function. Your configurations are saved with your preferences. To use a run configuration:

  1. Right-click on the function name and select Run Configurations > configuration name.

  2. The function executes according to the configuration you select and that configuration is the selected one the next time you use this context menu.

For more information about using run configurations, see Run MATLAB Files in the Editor.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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