Javatpoint Logo
Javatpoint Logo

MATLAB Environment

The 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 Desktop

When 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

  • Command Window
  • Command History Window
  • Start Button
  • Documents Window, containing the Editor/Debugger and the Array Editor
  • Figure Windows
  • Workspace Browser
  • Help Browser
  • Path Browser

We will discuss the functions of these tools, one by one.

Command Window

This 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 user-written 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.

MATLAB Environment

We will start working with the Command Window.

MATLAB Command Window Drop Downs-Show Command Window Actions

  • On the top bar, where 'Command Window' is written, there is a drop-down menu located on the right side; this is called 'Show Command Window Actions.'
MATLAB Environment
  • When you click on it, a drop-down menu appears with some options, like Clear Command Window, Find, Print, etc.
  • These options can be used for specific tasks, such as Clear Command Window option erases all the data from the command window, Find option can be used to find any word, letter, variable or function on the Command Window.

MATLAB Command Window Drop Downs-Browse for Functions

MATLAB Environment
  • On the left side, inside the command window, there is something visible in small fonts, and that is fx, an abbreviated form for function.
  • When you click on the fx, a drop-down search bar gets opened.
  • As shown in the image, these all are the name of the products we have installed.
  • Click on any of the products to get a list of all the related functions.
  • 'Browse for Functions' is a function search pop-up, and we can browse for the functions related to all the installed products.
  • The keyboard shortcut for the function search pop-up is Shift + F1.

MATLAB Command Window-Command Line

  • Here is the Command-Line started just in front of fx, indicated with the prompt, >>, two consecutive greater than signs, and a blinking cursor ahead.
MATLAB Environment
  • Use the command line to enter a variable, to call a function, or to express a calculation.

MATLAB Command Window-Working at Command Line

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

MATLAB Environment

i)

MATLAB Environment
  • Whenever we enter any character or word or sentence and press enter, it will show error like this-Undefined function or variable 'jdjdkjdk45'.
  • So the text should be enclosed either in double quotes (" ") or single quotes (' ').

ii)

MATLAB Environment

When we entered the above text, many things happened internally:

  • It creates a default variable, named as ans.
  • ans stands for the answer, so it is the answer or output of our command.
  • This variable is stored in the workspace and declared as String data type.
  • Whenever we enter this variable as a command, its previous stored value gets displayed as the output.
  • As this is the default variable, so whenever any valid command comes at the command line without providing any variable to hold the result, its value is stored in the ans variable only, and previous value gets replaced with the new one.

iii)

MATLAB Environment
  • This command will also output the same, but now it will be treated as a character data type.
  • The previous value of the ans variable gets replaced with the new one.
  • The data type of the ans variable now converted from string to character.

iv)

MATLAB Environment
  • This time the value of the ans variable will be updated with 84938.
  • And its data type also will be converted from previous character data type to number.
  • All types of numbers, integer or float or double, are stored as double internally.

v)

MATLAB Environment
  • This command suppresses the output, means the result calculated and stored in the variable, but its ouput doesn't show in the command window.
  • Here a variable with the name 'a' is created with the value 1988 and stored in the workspace.

vi)

MATLAB Environment
  • MATLAB is a case sensitive programming language, so the previous variable 'a' is different from the newly created variable 'A.'
  • Its value also saved in the workspace, and the output suppressed.

Let's take one more example:

MATLAB Environment
  • A new variable, 'b' will be created.
  • And it will be assigned value of the result of the calculation of 2+2, i.e., 4.
  • It gets saved in the workspace, and the output displayed on the command window.

Recalling Commands in MATLAB

  • We can recall previous commands by pressing up and down navigation arrow keys on the keyboard.
  • Type the first character of a command and press the up arrow key, all previously entered commands shown there. Use side navigation key to edit the highlighted command or press enter on the highlighted command to get the output.
MATLAB Environment
  • Up arrow key also can be used on the empty command line.

Command History Window

All 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 double-clicking on it. We can also select a set of commands from this window and create an M-file with the right click of the mouse (and selecting the suitable option from the menu).

Figure Window

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

MATLAB Environment

Editor Window

This is where we write, edit, create and save our programs in files known as M-files. We can use any text editor to carry out these functions. MATLAB supports its built-in editor. However, we can use our editor by typing the standard file-editing 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.

MATLAB Environment

Docking and Undocking Windows

MATLAB 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 right-hand 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 right-hand corner includes a small button with an arrow pointing down and to the right . .If this button is clicked, the window will be re-docked with the desktop.

MATLAB Environment

MATLAB Workspace

A 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, M-file, 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 M-files calc_area and sin_x are executed, the whos command will display the following list of variables:

          whos

Name Size Bytes Class Attributes
area 1x1 8 double
radius 1x1 8 double
string 1x32 64 char
x 1x61 488 double
y 1x61 488 double

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 Help

Online Documentation

MATLAB gives online help for all its built-in functions and programming language constructs. The command lookfor, help, helpwin, and helpdesk implement on-line 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.

Input-Output

MATLAB 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 input-output:

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 case-sensitive for variable names and built-in functions. Thus a and A are different variables. Some MATLAB commands and built-in 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 on-line help commands.

  • Paged output: To direct MATLAB to display one screen of output at a time, type more on at the MATLAB prompt. MATLAB flushes the completed output at once, without regard to the speed at which we read.
  • Output format: Through calculation inside MATLAB are achieve using double-precision, the presence of floating-point numbers on display is controlled by the output format in use. There are different screen output formats.

The following table presents the printed value of 10π in seven different formats.

format short 31.4159
format short e 3.1416e+001
format long 31.41592653589793
format long e 3.141592653589793e+001
format short g 31.416
format long g 31.4159265358979
format hex 403f6a7a2955385e
format rat 3550/113
format bank 31.42

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 types

MATLAB can read and write different types of files for saving data or programs that we are likely to use often:

M-files

M-files are standard ASCII text data, with a .m extension to the filename.

There are two types of M files:

  • Script Files
  • Function Files

Most programs we write in MATLAB are saves as M-files. All built-in functions in MATLAB are M-files, most of which reside on our computer in a precompiled format. Some built-in functions are supported with source code in readable M-files so that they can be copied and modified.

MAT-files

MAT-files are binary information files with a .mat extension to the file name. Mat-files 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. Mat-files can be loaded into MATLAB with the load commands.

Fig-Files

Fig-files 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 fig-file contains all the data required to recreate the figure. Specific files can be opened with the open filename.fig command.

P-files

P-files 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 (M-file), then you provide them the corresponding p-code or the p-file.

Mex-files

Mex-files are MATLAB-callable FORTRAN, C, and Java programs, with a .mex extension to the filename.






Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA