You are on page 1of 42

Matlab Programming (Part I)

Iulian Nastac
IAMSR

Presentation
MATLAB (produced by MathWorks Inc.) is a highperformance language for technical computing. The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix software developed by the LINPACK and EISPACK projects. Today, MATLAB engines incorporate the LAPACK and BLAS libraries, embedding the state of the art in software for matrix computation.
IAMSR
2

Introduction
MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar noninteractive language such as C, Pascal or Fortran. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.
IAMSR
3

Typical uses include


Math and computation Algorithm development Data acquisition Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building
IAMSR
4

Evolution
MATLAB has evolved over a period of years with input from many users. In university environments, it is the standard instructional tool for introductory and advanced courses in mathematics, engineering, and science. In industry, MATLAB is the tool of choice for high-productivity research, development, and analysis.
IAMSR
5

Matlab in Programming Languages Classifications


1. Depending on the programming level:
Low level (assembling instruction, Assembler) Medium level (C, Forth, etc.) High level (Matlab, Mathematica, Maple, etc.)

2. Depending on the output result:


Based on compiler (C, C++, Pascal, etc.) Based on interpreter (Matlab, Spice, CadStar, etc.)
IAMSR
6

Toolboxes
MATLAB features a family of add-on applicationspecific solutions called toolboxes. Very important to most users of MATLAB, toolboxes allow you to learn and apply specialized technology. Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the MATLAB environment to solve particular classes of problems. Areas in which toolboxes are available include control systems, neural networks, fuzzy logic, wavelets, simulation, economics and many others.
IAMSR
7

The MATLAB System


Development Environment The MATLAB Mathematical Function Library The MATLAB Language Graphics The MATLAB Application Program Interface (API)
IAMSR
8

Development Environment
A set of tools and facilities that help you use MATLAB functions and files. Many of these tools are graphical user interfaces. It includes the MATLAB desktop and Command Window, a command history, an editor and debugger, and browsers for viewing help, the workspace, files, and the search path.
IAMSR
9

10

IAMSR

11

The MATLAB Mathematical Function Library


A vast collection of computational algorithms ranging from elementary functions like sum, sin, cos, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms.
IAMSR
12

The MATLAB Language


A high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. It allows both "programming in the small" to rapidly create quick and dirty throw-away programs, and "programming in the large" to create complete large and complex application programs.
13

IAMSR

Graphics
MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as annotating and printing these graphs. It includes high-level functions for two-dimensional and three-dimensional data visualization, image processing, animation, and presentation graphics. It also includes low-level functions that allow you to fully customize the appearance of graphics as well as to build complete graphical user interfaces on your MATLAB applications.
14

IAMSR

The MATLAB Application Program Interface (API)


This is a library that allows you to write C and Fortran programs that interact with MATLAB. It includes facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a computational engine, and for reading and writing MAT-files.
15

IAMSR

Getting Started
MATLAB Desktop
Desktop Tools Command Window Command History Start Button and Launch Pad Help Browser Current Directory Browser Workspace Browser Array Editor Editor/Debugger Profiler
16

IAMSR

Command Window

17

IAMSR

Command History

18

IAMSR

Start Button and Launch Pad


The MATLAB Start button provides easy access to tools, demos, and documentation. The Launch Pad provides similar access in a tree view.

19

IAMSR

Help Browser

20

IAMSR

Current Directory Browser

21

IAMSR

Workspace Browser

22

IAMSR

Array Editor
Double-click a variable in the Workspace browser to see it in the Array Editor.

23

Editor/Debugger
Use the Editor/Debugger to create and debug M-files, which are programs you write to run MATLAB functions. The Editor/Debugger provides a graphical user interface for basic text editing, as well as for M-file debugging.

24

IAMSR

Profiler
A graphical user interface to help you improve the performance of your M-files.

25

Running MATLAB Functions


The Command Window is the main way you communicate with MATLAB. It appears in the desktop when you first start MATLAB. Use the Command Window to run MATLAB functions (also referred to as commands) and perform MATLAB operations.
26

IAMSR

Running Functions and Entering Variables


Entering Statements at the Command Line Prompt
The prompt (>>) in the Command Window indicates that MATLAB is ready to accept input from you. This prompt and your input are known as the command line. For example, to create A, a 3-by-3 matrix, type A = [1 2 3; 4 5 6; 7 8 10] When you press the Enter, the response is: A= 1 2 3 4 5 6 7 8 10 To run a function, type the function including all arguments and press Return. For example, type: magic(2) and MATLAB returns ans = 1 3 4 2 If you want to enter multiple lines before running, use Shift+Enter after each line 27 until the last. Then press Enter to run all of the lines.

Evaluating a Selection
To run a selection in the Command Window, make the selection, and then right-click and select Evaluate Selection from the context menu. Alternatively, after making a selection, press Enter. You cannot evaluate a selection while MATLAB is busy, for example, running an M-file.
28

IAMSR

Opening a Selection
You can open a function, file, variable, or Simulink model from the Command Window. Select the name in the Command Window, and then right-click and select Open Selection from the context window. This runs the open function for the item you selected so that it opens in the appropriate tool: M-files and other text files open in the Editor Figure files (.fig) open in a figure window Variables open in the Array Editor Models open in Simulink
29

IAMSR

Hyperlinks to Run Functions


Use matlab: to create a hyperlink for specified text, which runs the specified function when clicked. For example, typing disp('<a href="matlab:magic(4)">Generate magic square</a>') displays Generate magic square When the user clicks the link "Generate magic square", MATLAB runs magic(4).
30

IAMSR

Running One Process


In MATLAB, you can only run one process at a time. If MATLAB is busy running one function, any commands you issue will be buffered in a queue. The next command will run when the previous one finishes.
31

IAMSR

Running Programs
Running M-Files
Run M-files, files that contain code in the MATLAB language, the same way that you would run any other MATLAB function. Type the name of the M-file in the Command Window and press Enter. The M-file must be in the MATLAB current directory or on the MATLAB Search Path. To display each function in the M-file as it executes, use the Display preference and check Echo on, or use the echo function set to on.
32

IAMSR

Interrupting a Running Program


You can interrupt a running program by pressing Ctrl+C or Ctrl+Break at any time. On Windows platforms, you may have to wait until an executing built-in function or MEXfile has finished its operation. On UNIX systems, program execution will terminate immediately.
33

IAMSR

Running External Programs


The exclamation point character, !, is a shell escape and indicates that the rest of the input line is a command to the operating system. Use it to invoke utilities or run other programs without quitting from MATLAB. On UNIX, for example, !vi yearlystats.m invokes the vi editor for a file named yearlystats.m. After the program completes or you quit the program, the operating system returns control to MATLAB.
34

IAMSR

Opening M-Files
To open an M-file, select the file or function name in the Command Window, and then right-click and select Open Selection from the context window. The M-file opens in the Editor/Debugger. As an alternative use open or edit to open a file in the Editor. Use type to display the M-file in the Command Window.
35

IAMSR

Getting Help
The MathWorks provides online help and demonstrations for all products through the:
Help browser Help functions (dbtype, demo, help, helpdesk, lookfor, etc.) Other methods:
Downloading M-Files Contacting Technical Support Providing Feedback
36

IAMSR

Matrices
In MATLAB, a matrix is a rectangular array of numbers. Special meaning is sometimes attached to 1-by-1 matrices, which are scalars, and to matrices with only one row or column, which are vectors. MATLAB has other ways of storing both numeric and nonnumeric data, but in the beginning, it is usually best to think of everything as a matrix. The operations in MATLAB are designed to be as natural as possible. Where other programming languages work with numbers one at a time, MATLAB allows you to work with entire matrices quickly and easily.
37

IAMSR

Entering Matrices
You can enter matrices into MATLAB in several different ways:
Enter an explicit list of elements. Load matrices from external data files. Generate matrices using built-in functions. Create matrices with your own functions in M-files.
38

IAMSR

M-File Programming
M-files can be scripts that simply execute a series of MATLAB statements, or they can be functions that also accept arguments and produce output. You create M-files using a text editor, then use them as you would any other MATLAB function or command.
39

IAMSR

Kinds of M-Files
Script M-Files
Do not accept input arguments or return output arguments Operate on data in the workspace Useful for automating a series of steps you need to perform many times

Function M-Files
Can accept input arguments and return output arguments Internal variables are local to the function by default Useful for automating a series of steps you need to perform many times
40

IAMSR

Scripts
Scripts are the simplest kind of M-file because they have no input or output arguments. They're useful for automating series of MATLAB commands, such as computations that you have to perform repeatedly from the command line. Scripts operate on existing data in the workspace, or they can create new data on which to operate. Any variables that scripts create remain in the workspace after the script finishes so you can use them for further computations.
41

IAMSR

Script Example
% An M-file script to produce % Comment lines % "flower petal" plots theta = -pi:0.01:pi; % Computations rho(1,:) = 2*sin(5*theta).^2; rho(2,:) = cos(10*theta).^3; rho(3,:) = sin(theta).^2; rho(4,:) = 5*cos(3.5*theta).^3; for k = 1:4 polar(theta,rho(k,:)) % Graphics output pause end
42

IAMSR

You might also like