KEMBAR78
Workspace Variables Commands: Scientific Report | PDF | Matlab | File Format
0% found this document useful (0 votes)
67 views16 pages

Workspace Variables Commands: Scientific Report

The document discusses MATLAB workspace and variable preferences and how to optimize workspace performance. It explains how to configure options for saving workspace variables, set limits on array sizes, and specify whether to include NaN values in statistical calculations displayed in the workspace browser. The document also provides details on using the pack command to consolidate workspace memory and improve performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views16 pages

Workspace Variables Commands: Scientific Report

The document discusses MATLAB workspace and variable preferences and how to optimize workspace performance. It explains how to configure options for saving workspace variables, set limits on array sizes, and specify whether to include NaN values in statistical calculations displayed in the workspace browser. The document also provides details on using the pack command to consolidate workspace memory and improve performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Ministry of Higher Education and

Scientific Research
University of Technology
Petroleum Technology Department

Scientific Report

Workspace Variables Commands

Rana Nawar Sabah


2nd Stage (2020/2021)
Teacher Dr. sundus mahdi
Workspace and Variable Preferences
Workspace Preferences
Workspace preferences enable you to configure options for saving workspace
variables to MATLAB scripts. They also enable you to restrict the size of
®

arrays on which MATLAB performs calculations, and to specify if you want


those calculations to include or ignore NaNs.
To open Workspace preferences, on the Home tab, in
the Environment section, click Preferences.
Select MATLAB > Workspace. Workspace preferences are not available
in MATLAB Online™.
Preference Usage
Threshold for Specify Maximum array size to limit the number of elements of arrays saved to a
saving variables MATLAB script.
to MATLAB
script Specify Maximum struct/object nesting levels to limit the nesting level of
structures, arrays, or objects saved to a MATLAB script.
Multidimensional Specify how multidimensional arrays are formatted when saved to a MATLAB
array formatting script.
To create a compact script, select Row vector with reshape (compactness and
efficiency).
To retain array representation, select As 2-D pages (readability). Specify the
dimensions of a 2-D slice as positive integers less than or equal to the dimensions of
the n-D array. The second integer must be greater than the first.
File formatting Set the character width at which text in the MATLAB script is wrapped by
specifying Maximum characters per line.
n element and Limit the size of arrays for which the Workspace browser displays statistics to
smaller arrays improve performance when MATLAB updates the statistical results in the
show statistics Workspace browser.
For more information, see Improve Workspace Browser Performance During
Statistical Calculations.
Handling NaN Specify whether NaN values are included or excluded from calculations for the
values in statistics displayed in the Workspace browser. You can select to either Use NaNs
calculations when calculating statistics or Ignore NaNs when calculating statistics.
MATLAB array By default, MATLAB can use up to 100% of the size of RAM (not including virtual
size limit memory) of your computer to allocate memory for each MATLAB array. To change
this limit to a smaller percentage, select the Limit the maximum array size to a
percentage of RAM check box. Then move the slider to adjust the percentage of
RAM.
To allow MATLAB to use both RAM and virtual memory when creating an array,
clear the Limit the maximum array size to a percentage of RAM check box. If
MATLAB attempts to allocate memory that exceeds the resources available on the
computer, your system might become nonresponsive.
This limit applies to the size of each array, not the total size of all MATLAB arrays.

1
Variables Preferences
When working in the Variables editor, Variables preferences enable you to
specify array formatting, cursor movement, and the decimal separator for
exporting data using the system clipboard.
To open Variables preferences, on the Home tab, in
the Environment section, click Preferences.
Select MATLAB > Variables.
Preference Usage
Format Select an option from the Default array format to specify the default array output
format of numeric values displayed in the Variables editor. This format preference affects
only how numbers display, not how MATLAB computes or saves them. For information
on formatting options, see the reference page for the format function.
Editing Specify where the cursor moves to after you type an element and press Enter.
To keep the cursor in the element where you typed, clear the Move selection after
Enter check box.
To move the cursor to another element, select the Move selection after Enter check box.
In the Direction field, specify how you want the cursor to move.
International In the Decimal separator for exporting numeric data via system clipboard field,
number specify the decimal separator for numbers you cut or copy from the Variables editor and
handling then paste into text files or other applications.
This preference has no effect on numeric data copied from and pasted into MATLAB.
Within MATLAB, decimal separators are always periods.
Most Variables preferences are not available in MATLAB Online. To show
generated code in the Command Window when performing operations in
the MATLAB Online Variables Editor, select Show MATLAB code for
operations.
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.

2
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.

3
pack
Consolidate workspace memory
Syntax
pack
pack filename
pack('filename')
Description
pack frees up needed space by reorganizing information so that it only uses
the minimum memory required. All variables from your base and global
workspaces that are less than 2GB are preserved. Any persistent variables that
are defined at the time are set to their default value (the empty matrix, []).
Caution
The pack function is unable to preserve variables larger than 2GB. It clears
them from your workspace.
Do not use the pack function with objects that define events and listeners.
The pack function causes the destructor of any listeners defined for the
objects in the workspace. For information on restoring listeners when saving
objects.
The MATLAB® software temporarily stores your workspace data in a file
called tp######.mat (where ###### is a numeric value) that is located in
your temporary folder. (You can use the command dir(tempdir) to see the
files in this folder).
pack filename frees space in memory, temporarily storing workspace data in
a file specified by filename. This file resides in your current working folder
and, unless specified otherwise, has a .mat file extension.
pack('filename') is the function form of pack.
Examples
Change the current folder to one that is writable, run pack, and return to the
previous folder.

4
cwd = pwd;
cd(tempdir);
pack
cd(cwd)
Tips
You can only run pack from the MATLAB command line.
If you specify a filename argument, that file must reside in a folder for
which you have write permission.
The pack function does not affect the amount of memory allocated to the
MATLAB process. You must quit MATLAB to free up this memory.
Since MATLAB uses a heap method of memory management, extended
MATLAB sessions may cause memory to become fragmented. When
memory is fragmented, there may be plenty of free space, but not enough
contiguous memory to store a new large variable.
If you get the Out of memory message from MATLAB, the pack function
may find you some free memory without forcing you to delete variables.
The pack function frees space by
 Saving all variables in the base and global workspaces to a temporary
file.
 Clearing all variables and functions from memory.
 Reloading the base and global workspace variables back from the
temporary file and then deleting the file.
If you use pack and there is still not enough free memory to proceed, you
must clear some variables. If you run out of memory often, you can allocate
larger matrices earlier in the MATLAB session and use these system-
specific tips.
 When running MATLAB on The Open Group UNIX® platforms, ask
your system manager to increase your swap space.
 On Microsoft® Windows® platforms, increase virtual memory using
the Windows Control Panel.
5
save
Save workspace variables to filecollapse all in page
Syntax
save(filename)
save(filename,variables)
save(filename,variables,fmt)
save(filename,variables,version)
save(filename,variables,version,'-nocompression')
save(filename,variables,'-append')
save(filename,variables,'-append','-nocompression')
save filename
Description Of Syntax
save(filename) saves all variables from the current workspace in a
MATLAB® formatted binary file (MAT-file) called filename. If filename
exists, save overwrites the file.
save(filename,variables) saves only the variables or fields of a structure
array specified by variables.
save(filename,variables,fmt) saves in the file format specified by fmt. The
variables argument is optional. If you do not specify variables, the save
function saves all variables in the workspace.
save(filename,variables,version) saves to the MAT-file version specified by
version. The variables argument is optional.
save(filename,variables,version,'-nocompression') saves the variables to the
MAT-file without compression. The '-nocompression' flag only supports
MAT-file Version 7 (default) and Version 7.3. Therefore, you must specify
version as '-v7' or '-v7.3'. The variables argument is optional.
save(filename,variables,'-append') adds new variables to an existing file. If a
variable already exists in a MAT-file, then save overwrites it with the value
in the workspace.
6
For ASCII files, '-append' adds data to the end of the file.
To append to a Version 6 MAT-file, you must also include '-v6' as an input
argument.
save(filename,variables,'-append','-nocompression') adds new variables to an
existing file without compression. The existing file must be a MAT-file
Version 7 (default) or 7.3.
save filename is the command form of the syntax. Command form requires
fewer special characters. You do not need to type parentheses or enclose the
input in single or double quotes. Separate inputs with spaces instead of
commas.
Example
Save All Workspace Variables to MAT-File
Save all variables from the workspace in a binary MAT-file, test.mat. If
filename is a variable, use function syntax.
filename = 'test.mat';
save(filename)

7
saveas
Save figure or model using specified format
Syntax
saveas(h,'filename.ext')
saveas(h,'filename','format')

Description
saveas(h,'filename.ext') saves the figure or model with the handle h to the
file filename.ext. The format of the file is determined by the extension, ext.
Allowable values for ext are listed in this table.

ext Values Format

ai Adobe Illustrator `88

bmp Windows bitmap

emf Enhanced metafile

eps EPS Level 1

fig MATLAB figure (invalid for MATLAB models)

jpg JPEG image (invalid for MATLAB models)

m MATLAB M-file (invalid for MATLAB models)

pbm Portable bitmap

pcx Paintbrush 24-bit

pgm Portable Graymap

png Portable Network Graphics

ppm Portable Pixmap

tif TIFF image, compressed

8
saveas(h,'filename','format') saves the figure or model with the handle h to the
file called filename using the specified format. The filename can have an
extension but the extension is not used to define the file format. If no extension
is specified, the standard extension corresponding to the specified format is
automatically appended to the filename.
Allowable values for format are the extensions in the table above and the
device types supported by print. The print device types include the formats
listed in the table of extensions above as well as additional file formats. Use
an extension from the table above or from the list of device types supported
by print. When using the print device type to specify format for saveas, do not
use the prepended -d.
Remarks
You can use open to open files saved using saveas with an m or fig extension.
Other formats are not supported by open. The Save As dialog box you access
from the figure window's File menu uses saveas, limiting the file extensions
to m and fig. The Export dialog box you access from the figure
window's File menu uses saveas with the format argument.
Example
Specify File Extension
Save the current figure that you annotated using the Plot Editor to a file
named pred_prey using the MATLAB fig format. This allows you to open
the file pred_prey.fig at a later time and continue editing it with the Plot
Editor.
 saveas(gcf,'pred_prey.fig')

9
size
Array dimensions
Syntax
d = size(X)
[m,n] = size(X)
m = size(X,dim)
[d1,d2,d3,...,dn] = size(X)
Description
d = size(X) returns the sizes of each dimension of array X in a vector d
with ndims(X) elements.
[m,n] = size(X) returns the size of matrix X in separate variables m and n.
m = size(X,dim) returns the size of the dimension of X specified by
scalar dim.
[d1,d2,d3,...,dn] = size(X) returns the sizes of the first n dimensions of
array X in separate variables.
If the number of output arguments n does not equal ndims(X), then for:
n > ndims(X)
size returns ones in the "extra" variables, i.e., outputs ndims(X)+1
through n.
n < ndims(X)
dn contains the product of the sizes of the remaining dimensions of X,
i.e., dimensions n+1 through ndims(X).
Note For a Java array, size returns the length of the Java array as the
number of rows. The number of columns is always 1. For a Java array of
arrays, the result describes only the top level array.

10
Example
The size of the second dimension of rand(2,3,4) is 3.
m = size(rand(2,3,4),2)
m=
3
Here the size is output as a single vector.
d = size(rand(2,3,4))
d=
2 3 4
Here the size of each dimension is assigned to a separate variable.
[m,n,p] = size(rand(2,3,4))
m=
2
n=
3
p=
4

11
who, whos
who lists the variables currently in the workspace.
whos lists the current variables and their sizes and types. It also
reports the totals for sizes.
Graphical Interface
As an alternative to whos, use the Workspace browser.
Syntax
 who
 whos
 who('global')
 whos('global')
 who('-file','filename')
 whos('-file','filename')
 who('var1','var2',...)
 whos('var1','var2',...)
 who('-file','filename','var1','var2',...)
 s = who(...)
 s = whos(...)
 who -file filename var1 var2 ...
 whos -file filename var1 var2 ...

Description
who lists the variables currently in the workspace.
whos lists the current variables and their sizes and types. It also reports the
totals for sizes.
who('global') and whos('global') list the variables in the global workspace.

12
who('-file','filename') and whos('-file','filename') list the variables in the
specified MAT-file filename. Use the full path for filename.
who('var1','var2',...) and whos('var1','var2',...) restrict the display to the
variables specified. The wildcard character * can be used to display
variables that match a pattern. For example, who('A*') finds all variables in
the current workspace that start with A.
who('-file','filename','var1','var2',...) and
whos('-file','filename','var1','var2',...) list the specified variables in the MAT-
file filename. The wildcard character * can be used to display variables that
match a pattern.
s = who(...) returns a cell array containing the names of the variables in the
workspace or file and assigns it to the variable s.
s = whos(...) returns a structure with these fields
 name variable name
 size variable size
 bytes number of bytes allocated for the array
 class class of variable
and assigns it to the variable s.
who -file filename var1 var2 ... and whos -file filename var1 var2 ... are the
unquoted forms of the syntax.
Example
List the names of variables stored in the sample MAT-file durer.mat.
who -file durer.mat
Your variables are:
X caption map

13
workspace
Display the Workspace browser, a tool for managing the workspace

Graphical Interface
As an alternative to the workspace function, select Workspace from
the View menu in the MATLAB desktop.

Syntax
 workspace

Description
workspace displays the Workspace browser, a graphical user interface that
allows you to view and manage the contents of the MATLAB workspace. It
provides a graphical representation of the whos display, and allows you to
perform the equivalent of the clear, load, open, and save functions.

To see and edit a graphical representation of a variable, double-click the


variable in the Workspace browser. The variable is displayed in the Array
Editor, where you can edit it. You can only use this feature with numeric
arrays.

14
References
[1] https://www.mathworks.com/help/matlab/getting-started-with-matlab.html
[2] http://www.ece.northwestern.edu/local-
apps/matlabhelp/techdoc/ref/workspace.html

15

You might also like