You are on page 1of 9

--- ------

3

The MATLAB Desktop

As stated in the preceding chapter, running MATLAB creates one or more windows on your computer monitor. One of these windows. entitled M A TLA B, is commonly called the MATLAB desktop. This window contains or manages ali other windows that are part of MATLAB. Depending on how you set up MATLAB, some windows associated with the desktop mayor may not be visible. and some mayor may not reside within (i.e., be docked in) the MATLAB window. Management of the MATLAB desktop window and its associated windows is not discussed in this text. If you are familiar with other window-based programs, manipulation of windows in MATLAB will be familiar to you. The MATLAB desktop menu items change. depending on which window is active. In addition, there are many helpful contextual menus (which are accessed by pressing the right mouse button over an item). If you are not familiar with window-based programs. it may be beneficial to seek general assistance in this area from other sources. In any case, to find out which windows are associated with the desktop. investigate the items on the Desktop menu within the MATLAB desktop window.

3,1 MATLAB WINDOWS

The windows used in MATLAB include the (1) Command. (2) Command Historv, (3) Current Directory (browser). (4) Workspace (browser). (5) Help (browser). (6) Editor. and (7) Profiler.Tae following table gives an overview of the purpose of each of these windows:

21

._ .. --- ._--------

22

Chapter 3

The MATLAB Desktop

Window Description
Command Issues commands to MATLAB for processing
Command History Running history of prior commands issued in the Command window
Current Directory GUJ for directory and file manipulation in MATI.AB
Workspace GUI for viewing, editing, loading, and saving MATLAB variables
Help GUI for finding and viewing on-line documentation
Editor Text editor for creating M-files
Profiler Tool for optimizing M-fiIe performance 3.2 MANAGING THE MATLAB WORKSPACE

Within the MATLAB desktop, actions taken in all windows support computations performed in the Command window. As a result, the rest of this chapter provides more detailed information about the Command window.

The data and variables created in the Command window reside in what is called the MATLAB workspace or Base workspace. In addition to viewing variables in the Workspace window, you can see what variable names exist in the MATLAB workspace by issuing the command who:

» who
Your variables are:
angle_cl c4 cost pads
ans cS deg_cl real _cl
average_cost c6 erasers tape
cl c6i ima9_cl
c2 c6r items
c3 check - it_out mag_cl The variables you see may differ from those just listed, depending on what you've asked MATLAB In do since you opened the program. For more detailed information, use the command whos:

» whos Name angle_cl ans

Size lxl lx]

Bytes (lass

8 double array 8 double array

Section 3.2 Managing the MATLAB Workspace 23
average_cost Ixi 8 double array
cl lxl 16 double array (complex)
c2 1x1 16 double array (complex)
c3 lxl 16 double array (complex)
c4 Ixl 16 double array (complex)
c5 lxl 16 double array (complex)
c6 lxl 16 double array (complex)
c6i Ixl 8 double. array
c6r lxl 8 double array
check it out lxl 8 d.ouble array
cost lxl 8 double array
deg_cl !xl 8 double array
erasers lxl 8 double array
imag_cl Ixl 8 double array
items Ix1 8 double array
mag_cl Ixl 8 double array
pads lxI 8 double array
real c1 Ixl 8 double array
tape Ix1 8 double array
Grand total is 21 elements using 216 bytes Here. each variable is listed. along with its size. the number of bytes used, and its class. Since MATLAB is array oriented. all of the variahles belong to the class of double-precision arrays. even though all of the above variables are scalars. Later. as other data types or classes are introduced. the information in this last column will become more useful. The preceding list is also displayed in the Workspace window. which can be viewed by typing workspace at the MATLAB prompt or by choosing the Workspace menu item on the Desklop menu of the MATLA B desktop.

The command clear deletes variables from the MATLAB workspace. For example.

» clear real_cl imag_c1 c* » who

Your variables are:

angle_ c1 deg_cl mag_ cl
2.J15 erasers pads
&verage_ cost ~tems tape -

24

Chapter 3

The MAT LAB Desktop

deletes the varia hies rea 1_ c1, i mag_ e1, and all varia blesstarting wit h the letter c. Other options for the clear function can be identified by asking for information with the command hel p or hel pwi n:

» help clear

CLEAR Clear variables and functions from memory.

CLEAR removes all variables from the workspace. CLEAR VARIABLES does the same thing.

CLEAR GLOBAL removes all global variables.

CLEAR FUNCTIONS removes all compiled M- and MEX-functions.

CLEAR ALL removes all variables, globals, functions and MEX links. CLEAR ALL at the command prompt also removes the Java packages import list.

CLEAR IMPORT removes the Java packages import list at the command prompt. It cannot be used in a function.

CLEAR CLASSES is the same as CLEAR All except that class definitions are also cleared. If any objects exist outside the workspace (say in userdata or persistent in a locked m-file) a warning will be issued and the class definition will not be cleared. CLEAR CLASSES must be used if the number or names of fields in a class are changed.

CLEAR JAVA is the same as CLEAR ALL except that java classes on the dynamic java path (defined using JAVAClASSPATH) are also clEared.

CLEAR VARI VAR2 ... clears the variables specified. The wildcard character '.' can be used to clear variables that match a pattern. For instance, ClEAR X'" clears all the variables in the current workspace that start with X.

CLEAR -REGEXP PATl PAT2 can be used to match all patterns using regular expressions. This option only clears variables. For more information on using regular expressions, type "doc regexp" at the command prompt. If X is glObal, CLEAR X removes X from the current workspace, but leaves it accessible to any functions declaring it global.

CLEAR GLOBAL X completely removes the global variable X.

Section 3.3

Memory Management

25

CLEAR GLOBAL -REGEXP PAT removes global variables that match regular expression patterns.

Note that to clear specific global variables, the GLOBAL option must come first. Otherwise, all global variables will be cleared.

CLEAR FUN clears the function specified. If FUN has been locked by MLOCK it will remain in memory. Use a partial path (see PARTIAlPATH) to distinguish between different overloaded versions of FUN. For instance, 'clear inline/display' clears only the INLINE method for DISPLAY, leaving any other implementations in memory.

CLEAR ALL, CLEAR FUN, or CLEAR FUNCTIONS also have the side effect of removing debugging breakpoints and reinitializing persistent variables since the breakpoints for a function and persistent variables are cleared whenever the m-file changes or is cleared.

Use the functional form of CLEAR, such as CLEAR(' name '), when the variable name or function name is stored in a string.

Examples for pattern matching:

clear a* % Clear variables starting with "a"

clear -regexp Ab\d{3}$ % Clear variables starting with "b" and % followed by 3 digits

clear -regexp \d

% Clear variables containing any digits

See also who, whos, mlock, munlock, persistent.

Obviously, the c1 ear command does more than just delete variables. Its other uses will become apparent as you become familiar with more of MATLAB's features.

3.3 MEMORY MANAGEMENT

MATLAB allocates memory for variables as they are created and for M-file functions as they are used. Depending: on the computer on which the program is installed, it is possible for MATLAB to run out of memory. making it impossible to do any further work. When you eliminate variables by using the clear command. MATLAB frees up the memory used by the variables cleared. Over time. however. it is possible for rnernorv to become fragmented. leaving MATLAB·s rnernorv space

l_

,

26

Chapter 3

The MAT LAB Desktop

populated by variables surrounded by numerous small fragments of free memory. Since MATLAB always stores variables in contiguous chunks of memory, these fragments of free memory may not be reusable. To alleviate this problem, the pack command performs memory garbage collection. The command saves all MATLAB workspace variables to disk, clears all variables from the workspace, and then reloads the variables back into the workspace. On completion, all fragments of free memory are consolidated into one large, usable block. Depending on how much

memory is allocated to MATLAB on your computer, how long you've been running "1

a particular MATLAB session, and how many variables you've created. you mayor .

may not ever need to use the pack command. .

)

3.4 NUMBER DISPLAY FORMATS

When MATLAB displays numerical results, it follows several rules. By default, if a result is an integer, MATLAB displays it as an integer. Likewise, when a result is a real number, MATLAB displays it' ;with approximately four digits to the right of the decimal point. If the significant digits in the result are outside of this range. MATLAB displays the result in scientific notation, similar to the display of a scientific calculator. You can override this default behavior by specifying a different numerical format. From the Command window File menu, choose the Preferenees menu. Alternatively, type the appropriate MATLAB format command at the prompt. With the special variable pi, the numerical display formats produced by different format selections are as follows:

MATLAB pi Comments
Command
format short 3.1416 5 digits
format long 3.14159265358979 16 digits
format short e 3. 1416e+OOO 5 digits plus exponent
format long e 3. 1415926535897ge+OOO 16 digits plus exponent
format short g 3.1416 Best of format short or
format short e
format long 9 3.14159265358979 Best of format long or
format long e
format hex 400921fbS4442d18 Hexadecimal, floating point
format bank 3.14 2 decimal digits
format + + Positive (+), negative (-), or
zero (0)
format rat 355/113 Rational approximation Section 3.6

System Information

27

j'lrmat debug

m 1
n 1
pr lld60dO
pi 0
3.1416 Structure address=, 1214830 Internal storage information in addition to shortg

Note: In MATLAR Ihe internal representation of a number does not change when different display formats are chosen; only the display changes. All calculations using double-precision numbers are performed using double-precision arithmetic.

3.5 KEEPING A SESSION lOG

Sometimes it is useful to keep a log of all work performed during a MATLAB session. The Command History window keeps a chronological log of the functions and commands executed during current and past MATLAB work sessions, but does not show the results. MATLAB provides a command di ary. which saves all Command window activity to a text file in the current directory. The help text for di ary describes its use:

» help diary

DIARY Save text of M.ATLAB session.

DIARY filename causes a copy of all subsequent command window input and most of the resulting command window output tc be appended to the named file. If no file is specified, the file 'diary' is used.

DIARY OFF suspends it.

DIARY ON turns it back on.

DIARY, by itself, toggles the diary state.

3.6 SYSTEM INFORMATION

MATLAB provides a number of commands that provide information about the computer in use. as well as about the MATLAB version in use. The command computer returns a character string identifying the computer in use:

» computer

ans ~

PCi~/IN

i_

1

I I

I

i

r

,

!

28

Chapter 3

The MATlAS Desktop

In this case, the computer is a PC that is running Windows XP. The command version returns a character string identifying the MATLAB version:

» version

ans '"

7.0.0.151483 (R14)

Note that the command ver returns information about MATLAB, as well as installed toolboxes:

> ver

MATLAB Version 7.0.0.051483 (R14) MATlAB license Number: 9754

Operating System: Microsoft Windows XP Version 5.2 (Build 2600: Service Pack 1) Java VM Version: Java 1.4.2 ~th Sun Microsystems Inc. Java HotSpot(TM) ~lient VM

MATlAB

Mastering MATlAB Toolbox

Version 7.0 Version 6.0

(R14)

MA nAB licensing information can be found by using the commands 1 i-cense and hostid:

» hostid '9754'

>:> license

ans =

9754

Of course, your results from entering these commands will most likely be different from those shown because your computer and MATLAB version will probably differ from those used to produce this text.

3.7 THE MATlAB SEARCH PATH

MATLAB uses a search path to find information stored in files on disk. MATLAB's files are organized into numerous directories and subdirectories.

The list of all directories where MATLAS·s files are found is called the MATLAB search path or simply MATLAB path.

---~- . -- ~-~

Section 3.7

The MATLAB Search Path

29

Use of the MA TLA B search path is described next. When you enter cow at the MATLAB prompt by typing »cow. MATLAB does the following: (I) It checks 10 see if cow is a variable in the MATLAB workspace. (2) If not. it checks to see if cow is a built-in function. (3) If not. it searches for a file named cow.m in the current directory, (4) If none exists, it checks to see if cOW.1n exists anywhere On the MATLAB search path by searching in the order in which the path is specified. (5) If cow isn't found at this point. MATLAB reports an error.

MATLAB takes appropriate action according to this search strategy. If cow is a variable. MATLAB uses the variable. If cow is a built-in function. it is called. If cow.m is a file (either in the' current directory or anywhere on the MATLAB search path) the file cow.m is opened, and MATLAB acts on what is found in the file. As is documented in Chapters 4 and ]2. MATLAB has two basic file types that are of common use: both are simple text files containing MATLAB commands. (See Chapters 4 and 12 for further information regarding these M~fi'es).

In reality, the search procedure is more complicated than that just described, because of advanced features in MATLAB: For the most part. however, this search procedure provides sufficient detail for basic MATLAB work. (More detailed information regarding the MATLAB search path can be found in Chapter 12.)

When MATLAB starts up, it defines a default MATLAB search path that points to all of the directories in which MATLAB stores its files. This search path can be displayed and modified in several ways. The easiest way is to use the Path Browser. which is a graphical user interface designed for viewing and modifying the MATLAB search path. The path browser is made available by choosing Set Path ... from the File menu on the MATLAB desktop window. Since the MATLAB search path already points to all directories where MATLAB stores its files. the primary purpose for accessing the path browser is to add your own MATLAS file storage directories to the search path.

To display the MATLAB search path in the Command window, MATLAB provides the function matlabpath. in addition. the features of the path browser can be duplicated in the Command window by using the functions path, addpath. and rmpath. For more information regarding these functions, see the on-line documentation.

You might also like