Chap#1_Introducation_to_MATLAB
Chap#1_Introducation_to_MATLAB
Dr. A. AMMAR
1. Introduction to MATLAB
2. Matrix computations and linear algebra
3. Programming Essentials in MATLAB
4. Introduction to SIMULINK
5. Simulink applications for power engineering
6. App Designer and Graphical User Interface
7. Extra Toolboxes
MATLAB’s interface and windows are customizable. Users can easily manipulate,
customize, and change preferences of the package according to their needs.
Command Window
Current Folder
Fig.1
01/20/2025 EE421 Dr. AMMAR 5
The default window has the main menu tools, a Current Directory indicator, and
Command, Workspace, and Command History windows.
Tools and toolbars are grouped into three menus HOME, PLOTS,
and APPs (see Fig.2).
The HOME tab contains all the main tools.
It’s where you can create and delete new files and variables, import data, access
the code analyzer, launch Simulink, and access addons and help options…etc
Fig.2
01/20/2025 EE421 Dr. AMMAR 6
• The Command Window is in the central pane (by default). All commands and/or
(small) scripts/codes can be entered directly.
• The Workspace pane (Fig.3) of the default desktop window shows the current
entries and saved variables during the session. These entries are saved
temporarily until the MATLAB application is closed. All essential attributes and
properties of entries/variables (variable names, values, and types) are displayed
in the workspace.
Fig.3
Work in MATLAB generally starts in the Command Window, but before you type any
command, it is worth noting the current directory. The current directory address can
be viewed directly from the main window (see Fig.4).
Fig.4
For short commands and calculations and/or to view attributes of the available
variables in the current directory, use the Command Window.
However, for series of commands and longer scripts, it is more efficient to use
script editors, such as the M-file and MLX-file editors.
MATLAB uses a default variable ans, short for answer, to store the results of the
current calculation.
To avoid this, you may assign a value to a variable or output argument name. For
example
Mathematical functions
MATLAB offers many predefined mathematical functions for technical computing
which contains a large set of mathematical functions.
Typing help elfun and help specfun calls up full lists of elementary and special
functions respectively
There is a long list of mathematical functions that are built into MATLAB. These
functions are called built-ins. Many standard mathematical functions, such as sin(x),
cos(x), tan(x), ex, ln(x), are evaluated by the functions sin, cos, tan, exp, and log
respectively in MATLAB.
The MATLAB application has a few files that are recognizable by their extensions.
They are .M, .MLX, .MAT, .BI, and .FIG.
MAT-files are used to save all types of variables available in the workspace and can
be accessed easily from M/MLX-files and the Command Window
In addition, the Simulink application has three types of files .mdl, .slx, and .slxc.
They are used to build and simulate Simulink models
Variable names must start with a letter and can be combined with any integer
numbers, such as 0, 1, 2, … 9 and (_) an underscore sign.
The maximum length of the variable name is 63 characters. MATLAB treats the
variables called a and A as two different variable.
Application :
Now let’s get started working in the Command Window by entering and
assigning variable names, performing simple some basic arithmetic operations, and
making changes to the output data formats.
A=3;
B=-2;
C=1/2;
D = -1.5;
ABCD=A^2+B/C+D;
If ";" is missed and the content of the variable ABCD will be displayed
ABCD = 3.5000
Type who or whos to display the list of variable names currently in memory.
01/20/2025 EE421 Dr. AMMAR 15
>> sqrt(ABCD)
ans = 1.8708
While all computations in MATLAB are done in double precision, the default format
prints results with five significant digits. The format of the displayed output can be
displayed using different format, such as format long, format short, format long
eng, format bank, format hexadecimal, format rational, etc.
ABCD = 3.5000
Results in
A text variable can be augmented with more text variables, for example
produces
The values and attributes of all variables entered in the Command Window will
be saved in the workspace until you clean up the workspace by deleting the
variables using the clear, clearvars, or clear all command
clc for cleaning up the Command Window and starting with a blank Command
Window
Let’s look at several examples on Fig.5 of how to generate various data types
supported in MATLAB:
• Numerical data
• Logical arrays
• Character arrays/variables
• Table arrays
• Cell arrays
• Structure arrays
• Function handles
• Classes and graphic handles
Fig.5
However, for memory efficiency and faster calculation purposes, other storage
formats can also be used. MATLAB supports single precision or integer type and
int8 … 64, uint8 …64 storage format types.
Table.5 shows how data can be saved in every storage class type and the
conversion function used in MATLAB for each type.
The M-file editor shows all errors in the Command Window after the M-file’s
execution. The M-file editor toolbar is shown in Fig.7
M-File Editor
We can push new script to open the M-file editor
Fig.7
01/20/2025 EE421 Dr. AMMAR 23
In the M-file editor’s main tools menu is shown in Fig. it is sub-grouped in three
tabs EDITOR, PUBLISH, and VIEW, as shown in Fig.8
In order to demonstrate some of the highlighted tools and options of the M-file.
let’s look at the following example
First, the solution script of this simple example will created in the M-file editor
Comments are not executable and contain additional information for the users. The
% sign is used to place comments and remarks or any additional information within
M-files and MLX-files.
Moreover, inserting %% at the beginning of a line and leaving a blank space after
it creates a cell mode in the script.
Tab. After pressing the button, the script is executed automatically and
its HTML-formatted report will be generated, as shown in Fig.
plot(x,y);
Fig.9
As we just saw, plotting is very easy in MATLAB. Any pair of vectors can be plotted
versus each other as long as both vectors have the same length.
However, the result is not a finished product, since there are no titles, axis labels, or
grid lines on the plot.
x = 0:1:10;
y = x.^2 – 10.*x + 15;
plot(x,y);
title ('Plot of y = x.^2 – 10.*x + 15');
xlabel ('x');
ylabel ('y');
grid on;
Fig.10
01/20/2025 EE421 Dr. AMMAR 31
In addition, the “File/Save As” menu option in the Figure Window can be used
to save a plot as a graphical image. In this case, the user selects the filename and
the type of image from a standard dialog box (Fig.11).
Fig.11
The character array can have up to three characters, with the first character
specifying the color of the line, the second character specifying the style of the
marker, and the last character specifying the style of the line.
Table .6
Note that all plotting option can be done also using the property editor
This chapter introduced the MATLAB environment, including its settings, variables,
common commands, and M-file tools. You also learned about assigning variables
and values from the Command Window and working in the M-file editor windows,
Finally we’ve seen some plotting options
The following summary lists all of the MATLAB special symbols, commands, and
functions described in this chapter, along with a brief description of each one.
To end your MATLAB session, type quit in the Command Window, or select
File>Exit MATLAB in the desktop main menu.