Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Create and Edit Variables

In MATLAB®, you can create, edit, and copy variables in the MATLAB workspace. The MATLAB workspace consists of the variables you create and store in memory during a MATLAB session.

View Workspace Contents

To view a list of variables in your workspace, use the Workspace browser. To open the Workspace browser if it is not currently visible, do either of the following:

  • On the Home tab, in the Environment section, click Layout. Then, under Show, select Workspace.

  • Type workspace in the Command Window.

By default, the Workspace browser displays the base workspace. You also can view function workspaces if MATLAB is in debug mode. For more information, see Base and Function Workspaces.

To display additional columns such as size and range, on the Workspace browser title bar, click , and then click Choose Columns.

You can also use the who command in the Command Window to view a list of variables. To list information about size and class, use the whos command.

Create Variables

You can create new variables in the workspace by running MATLAB code or using existing variables.

To create a new variable, enter the variable name in the Command Window, followed by an equal sign (=) and the value you want to assign to the variable. For example, if you run these statements, MATLAB adds the three variables x, A, and I to the workspace:

x = 5.71;
A = [1 2 3; 4 5 6; 7 8 9];
I = besseli(x,A);

You do not have to declare variables before assigning values to them.

If you do not end the assignment statement with a semicolon (;), MATLAB displays the result in the Command Window. For example,

x = 5.71
x =
    5.7100

If you do not explicitly assign the output of a statement to a variable, MATLAB generally assigns the result to the reserved word ans. The value of ans changes with every statement that returns an output value that is not assigned to a variable. For example,

sin(1)
ans =
    0.8415

To create a new workspace variable from an existing variable, in the Variables editor, select an element, data range, row, or column in an array, and then in the Variable tab, select New from Selection.

View Variable Contents

You can view the contents of a variable in several ways:

  • Command Window — Type the variable name at the command prompt.

  • Variables editor — In the Workspace browser, double-click a variable name. You can also use the openvar function. For example, to open the variable A, type openvar('A').

    Some variables open a viewer or other tool appropriate for the type of value they have, such as timeseries. For details, see the documentation for that data or object type.

    A or icon next to a variable property in the Variables editor indicates that the property is protected or private.

      Note:   The maximum number of elements in a variable that you can open in the Variables editor depends on your operating system and the amount of physical memory installed on your system.

    To change how the Variables editor displays variables, go to the View tab, and in the Format section, select a number display format. The display format does not affect how values are displayed in the Command Window or Workspace browser, or how the variables are saved.

Edit Variable Contents

You can edit the contents of scalar (1-by-1) variables in the Workspace browser. Right-click the variable and select Edit Value.

To edit other variables, open them in the Variables editor. For example, suppose that you create a cell array, C, by running these commands in the Command Window:

A = magic(4);
C = {A A A};
In the Workspace browser, double-click the variable name C to open it in the Variables editor.

To edit an element of a variable, double-click the element. The element opens in a new document within the Variables editor. For example, if you double-click element C{1,1} in the Variables editor, the contents of that cell open in a new tab. You can edit the value of a variable element by clicking the element and typing a new value. Press Enter or click another element to save the change.

To return to the parent cell array or structure of an element, go to the View tab and click the Go Up button.

Changes you make in the Variables editor are automatically saved in the workspace. Changes you make to variables via the Command Window or other operations automatically update the information for those variables in the Workspace browser and Variables editor.

    Note:  

    • You cannot edit elements or subsets of multidimensional arrays in the Variables editor.

    • You cannot edit tall arrays in the Variables editor.

    • When editing strings in the Workspace browser or as part of a structure in the Variables editor, you must use double quotes to surround the string value.

To modify the size, shape and order of variable elements in the Variable editor, use the following procedures:

ActionProcedure

Delete row, column, or variable elements

Right-click the desired row header, column header, or selected elements and select Delete Row or Delete Column.

Insert new row or column

Right-click the desired row header, column header, or element and select Insert Row Above, Insert Row Below, Insert Column to the Left, or Insert Column to the Right. You can also add rows or columns simply by entering a value in an empty row or column. For example, to add a row and column to the array in C{1,1}, enter a value in element (5,5).

Cut variable elements

Right-click the desired row header, column header, or selected elements and select Cut. The cut values move to the clipboard and are replaced by the default value for empty elements. For more information, see Empty Elements.

Copy variable elements

Right-click the desired row header, column header, or selected elements and select Copy.

Paste variable elements

Right-click the row header, column header, or element where you want the insertion to begin and select Paste.

Paste cells from Microsoft® Excel® spreadsheet

Right-click the element where you want the insertion to begin, and then select Paste Excel Data.

Empty Elements

Empty elements in variables are assigned default values. Default assignments are:

  • 0 for numeric arrays

  • [] for cell arrays and structure arrays

  • <undefined> for categorical variables

Edit Table and Structure Array Variables

Tables (including timetables) and structure arrays support additional editing actions.

ActionProcedureExample
Modify column or row nameDouble-click the name and enter the new text.

Reorder variablesHover over the left side of a variable until a four-headed arrow appears. Then, click and drag the column to a new location.

Modify units and description of variablesClick the arrow that appears to the right of a variable name. Then, enter new text in the Units and Description fields.

Sort variable dataClick the arrow that appears to the right of a variable name and select Ascending or Descending.

    Note:   The contents of a table are only visible and modifiable when the number of variables is fewer than 5000. When the number of variables equals or exceeds 5000, you can only view the table properties.

Changes made to certain variable types in the Variables editor also appear in the Command Window. For example, suppose you have a table T that contains three columns, A, B, and C. If you delete column A in the Variables editor, the line T(:,'A') = []; displays in the Command Window. To suppress code display in the Command Window, on the View tab, clear the Show MATLAB Code check box.

Navigate Variable Contents

When editing variables in the Variables editor, some variables can contain large amounts of data, making it difficult to navigate between elements. Use these keyboard shortcuts to move easily between variable elements in the Variables editor. You cannot modify these keyboard shortcuts.

Action

Keyboard Shortcut

Commit changes to element and move to next element.

Variables Preferences enable you to specify what the next element is. The default is to move down.

Enter

Move right.

Within a selection, Tab also moves from the last column in one row to the first column in the next row.

Tab

Move in opposite direction of Enter or Tab.

Shift+Enter or Shift+Tab

Move up m rows, where m is the number of visible rows.

Page Up

Move down m rows, where m is the number of visible rows.

Page Down

Move to column 1.

Home

Move to row 1, column 1.

Ctrl+Home

Edit current element, positioning cursor at the end of the element.

F2 (Ctrl+U on Apple Macintosh platforms)

Copy, Rename, and Delete Variables

You can copy and paste, duplicate, rename, and delete variables within the Workspace browser.

ActionProcedure

Copy variable to and from clipboard

Select the variables, right-click, and then select Copy. Then, you can paste the names, for example, into the Command Window or an external application. Multiple variables are comma-separated.

Duplicate variable

Select the variables, right-click, and then select Duplicate. MATLAB creates a copy of the selected variables.

Rename a workspace variable

Right-click the variable name, and then select Rename. Type the new variable name and press Enter.

Delete all variables in workspace

On the Home tab, in the Variable section, click Clear Workspace.

You can also use the clear function in the Command Window.

Delete selected variables from workspace

Select the variables in the Workspace browser, right-click, and then select Delete.

You can also use the clear function in the Command Window. For example, to clear variables A and B, use the command clear A B.

To preserve specified variables, but delete others, use the clearvars function with the -except option. For example, to clear all variables except variable A, use the command clearvars -except A.

You can change the character that delimits decimals in the data when you cut and paste values from the Variables editor into text files or other applications. You might do this, for instance, if you provide data to a locale that uses a character other than the period (.). To change the delimiter character, specify a Decimal separator for exporting numeric data via system clipboard in the Variables Preferences.

Display Statistics in the Workspace Browser

For each variable or object, the Workspace browser can display statistics such as the Min, Max, and Mean, when relevant. MATLAB performs these calculations using the min, max, and mean functions, and updates the results automatically.

To display statistics, on the Workspace browser title bar, click , and then select Choose Columns. Select the statistics you want MATLAB to calculate.

Improve Workspace Browser Performance During Statistical Calculations

If you show statistical columns in the Workspace browser, and you work with very large arrays, you might experience performance issues when the data changes as MATLAB updates the statistical results. To improve performance, consider one or both of the following:

  • Show only the statistics of interest to you.

    On the Workspace browser title bar, click , and then select Choose Columns. Clear the statistics you do not want MATLAB to calculate.

  • Exclude large arrays from statistical calculations.

    On the Home tab, in the Environment section, click Preferences. Select MATLAB > Workspace, and then use the arrow buttons under Statistical calculations to change the maximum array size for which the Workspace browser performs statistical calculations. Any variable exceeding the maximum array size reports <Too many elements> in Workspace browser statistics columns instead of statistical results.

Include or Exclude NaN Values in Statistical Calculations

If your data includes NaNs (Not-a-Number values), you can specify that the Workspace browser statistical calculations consider or ignore the NaNs. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Workspace, and then select one of the following:

  • Use NaNs when calculating statistics

    If a variable includes a NaN, and you select this option, the values for Min, Max, Var, and several other statistics appear as NaN. However, Mode and several other statistics show a numeric result.

  • Ignore NaNs whenever possible

    If a variable includes a NaN, and you select this option, numeric results appear for most statistics including Min, Max, and Mode. However, Var still appears as NaN.

More About

Was this topic helpful?