MATLAB EnvironmentThe structural unit of data in any MATLAB program is the array. An array is a collection of record values organized into rows and columns and known by a single name. Individual data values within an array may be accessed by including the name of the array, followed by subscripts in parentheses that identify the row and column of the particular value. Even scalars are acted as arrays by MATLAB: they are arrays with only one row and one column. When MATLAB executes, it can display various types of windows that accept commands or display data. There are three most important types of windows are: Command Windows: where commands may be entered Figure Windows: which display plots and graphs Edit Windows: This permits a user to create and modify MATLAB programs. MATLAB DesktopWhen we start MATLAB Version 7.4, a specific window known as the MATLAB desktop appears. The desktop is a window that includes other windows display MATLAB data, plus toolbars and a "Start" button similar to that used by Windows XP. By default, most MATLAB tools are "docked" to the desktop so that they develop inside the desktop window. However, the user can choose to "undock" any or all tools, making them appear in windows separate from the desktop. It integrates various tools for managing data, variables, and applications within the MATLAB environment. The main tools within or accessible from the MATLAB desktop are
We will discuss the functions of these tools, one by one. Command WindowThis is the main window. It is characterized by the MATLAB command prompt (>>). When you launch the function program, MATLAB puts you in this window. All commands containing those for running userwritten programs are typed in this window at the MATLAB prompt. In MATLAB, this window is a part of the MATLAB that contains other smaller windows or panes. Here is an image of the screen that appears when you open the MATLAB. We will start working with the Command Window. MATLAB Command Window Drop DownsShow Command Window Actions
MATLAB Command Window Drop DownsBrowse for Functions
MATLAB Command WindowCommand Line
MATLAB Command WindowWorking at Command LineType here anything you want. Anything entered at the Command Line is treated as a command. For example, there are six types of commands entered to explain their meaning. i)
ii) When we entered the above text, many things happened internally:
iii)
iv)
v)
vi)
Let's take one more example:
Recalling Commands in MATLAB
Command History WindowAll commands typed on the MATLAB prompt in the command window get documented, even across various sessions in this window. We can selected command from this window with the mouse and execute it in the command window by doubleclicking on it. We can also select a set of commands from this window and create an Mfile with the right click of the mouse (and selecting the suitable option from the menu). Figure WindowThe output of all graphics commands we copied in the command window is flushed to the graphics or figure window, a separate gray window with (default) white background color. The client can create as many figure windows as the system memory will allow. Editor WindowThis is where we write, edit, create and save our programs in files known as Mfiles. We can use any text editor to carry out these functions. MATLAB supports its builtin editor. However, we can use our editor by typing the standard fileediting commands that we usually use on our system. From within MATLAB, the command is typed at the MATLAB prompt, ensuring the exclamation character (!). The exclamation character prompts MATLAB to return the control temporarily to the local operating system, which executes the commands providing the character. After the editing is completed, the control is returned to the MATLAB. Docking and Undocking WindowsMATLAB windows such as the Command Window, the Edit Window, and Figure Windows can either be docked to the desktop, or they can be undocked. When a window is docked, it occurs as a pane within the MATLAB desktop. When it is undocked, it occurs as an independent window on the computer screen separate from the desktop. When a window is docked to the desktop, the upper righthand corner includes a small button with an arrow pointing up and to the right ↑ . If this button is clicked, the window will become an independent window. When the window is an independent window, the upper righthand corner includes a small button with an arrow pointing down and to the right ↓. .If this button is clicked, the window will be redocked with the desktop. MATLAB WorkspaceA statement such as z = 10 creates a variable named z, stores the value 10 in it, and store it in a part of computer memory known as the workspace. A workspace is the set of all the variables and arrays that can be used by MATLAB when a specific command, Mfile, or function is executing. All commands executed in the Command Window (as well as all script files executed from the Command Window) share a simple workspace, so they can all share variables. MATLAB operation differs from script files in that each operation has its separate workspace. A list of the variables and arrays in the current workspace can be developed with the whos command. For example, after Mfiles calc_area and sin_x are executed, the whos command will display the following list of variables: whos
Script file calc_area created variables area, radius, and string, and script file sin_x created variables x and y. Note: All the variables are in the same workspace, so if two script files are executed in succession, the second script file can use variables created by the first script file.The contents of any variable or array may be determined by typing the appropriate name in the Command Window. For example, the contents of the string can be found as follows: string string = The area of the circle is 19.635 A variable can be removed from the workspace with the clear command. The clear command takes the form clear var1 var2 ... where var1 and var2 are the names of the variables to be deleted. The command clear variables or simply clear remove all variables from the current workspace. Online HelpOnline Documentation MATLAB gives online help for all its builtin functions and programming language constructs. The command lookfor, help, helpwin, and helpdesk implement online help. Demo MATLAB has a display program that shows many of its features. Type demo at the MATLAB prompt to invoke the demonstration program, and follow the information on the screen. InputOutputMATLAB provides interactive computation taking the input from the screen and flushing the output to the screen. It can read input data and write output data. The following characteristics hold for all forms of inputoutput: Data type The component data type in MATLAB is an array. It encompasses various distinct data objects: integers, doubles (real numbers), matrices, character strings, structures, and cells. Dimensioning Dimensioning is automatic in MATLAB. No dimension statements are needed for vectors or arrays. We can find the dimension of a current matrix or a vector with the size and length (for vectors only) commands. Case Sensitivity MATLAB is casesensitive for variable names and builtin functions. Thus a and A are different variables. Some MATLAB commands and builtin function calls are typed in lowercase letters. We can turn case sensitivity on and off with the casesen command. Output Display The output of every command is presented on the screen unless MATLAB is directed otherwise. A semicolon at the end of a command contains the screen output, except for graphics and online help commands.
The following table presents the printed value of 10π in seven different formats.
The additional formats, format compact and format loose, control the spacing above and following the displayed lines, and format +displays a +, ,and blank for positive, negative, and zero numbers, respectively. The default is format short. File typesMATLAB can read and write different types of files for saving data or programs that we are likely to use often: Mfiles Mfiles are standard ASCII text data, with a .m extension to the filename. There are two types of M files:
Most programs we write in MATLAB are saves as Mfiles. All builtin functions in MATLAB are Mfiles, most of which reside on our computer in a precompiled format. Some builtin functions are supported with source code in readable Mfiles so that they can be copied and modified. MATfiles MATfiles are binary information files with a .mat extension to the file name. Matfiles are created by MATLAB when you save a record with save command. The record is written with a special format that only MATLAB can read. Matfiles can be loaded into MATLAB with the load commands. FigFiles Figfiles are binary figure files with a .fig extension that can be open again in MATLAB as figures. Such files are created by saving a figure in the format using the Save or Save As options from the File menu or using the saveas command in the command window. A figfile contains all the data required to recreate the figure. Specific files can be opened with the open filename.fig command. Pfiles Pfiles are compiled with a .p extension that can be executed in MATLAB directly (without being parsed and compiled). These files are created with the pcode command. If you develop an application that other person can use, but you do not want to provide them the source code (Mfile), then you provide them the corresponding pcode or the pfile. Mexfiles Mexfiles are MATLABcallable FORTRAN, C, and Java programs, with a .mex extension to the filename.
Next TopicWorking with Variables and Arrays
