Writing a MATLAB Program

  1. Using Command Window: Only one statement can be typed and executed at a time. It executes the statement when the enter key is pressed. This is mostly used for simple calculations. Note: ans is a default variable created by MATLAB that stores the output of the given computation.
  2. Using Editor: Multiple lines of code can be written here and only after pressing the run button (or F5) will the code be executed. It is always a good practice to write clc, clear and close all in the beginning of the program.Note: Statements ending with a semicolon will not be displayed in the command window, however, their values will be displayed in the workspace. Any statement followed by % in MATLAB is considered as a comment
  3. Vector Operations: Operations such as addition, subtraction, multiplication and division can be done using a single command instead of multiple loops

We can also extract separate rows and columns by using the colon(:) operator. Consider a matrix A of size 3X3. The following commands can be used to extract rows and columns from Matrix A

Command Description
A(:, n) To extract the elements of all rows in column n of the matrix
A(m, : ) To extract the elements of all columns in row m of the matrix
A(:, m:n) To extract the elements of all rows between columns m and n of the matrix
A(m:n, : ) To extract the elements of all columns between rows m and n of the matrix
A(p:q, m:n) To extract the elements of rows between p and q and columns between m and n of the matrix
A(m, n) To extract the elements of row m and column n

Introduction to MATLAB

MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It was developed by Cleve Molar of the company MathWorks.Inc in the year 1984.It is written in C, C++, Java. It allows matrix manipulations, plotting of functions, implementation of algorithms and creation of user interfaces.

Similar Reads

Getting Started with MATLAB

It is both a programming language as well as a programming environment. It allows the computation of statements in the command window itself....

Basic Functions in MATLAB

Function Description disp() The values or the text printed within single quotes is displayed on the output screen clear To clear all variables close all To close all graphics window clc To clear the command window exp(x) To compute the exponential value of x to the base e abs(x) To compute the absolute value of x sqrt(x) To compute the square root of x log(x) To compute the logarithmic value of x to the base e log10(x) To compute the logarithmic value of x to the base 10 rem(x, y) To compute the remainder of x/y sin(x) To compute the sine of x cos(x) To compute the cosine of x tan(x) To compute the tangent of x atan2(x, y) To compute the arctangent or inverse of y/x...

Writing a MATLAB Program

Using Command Window: Only one statement can be typed and executed at a time. It executes the statement when the enter key is pressed. This is mostly used for simple calculations. Note: ans is a default variable created by MATLAB that stores the output of the given computation. Using Editor: Multiple lines of code can be written here and only after pressing the run button (or F5) will the code be executed. It is always a good practice to write clc, clear and close all in the beginning of the program.Note: Statements ending with a semicolon will not be displayed in the command window, however, their values will be displayed in the workspace. Any statement followed by % in MATLAB is considered as a comment Vector Operations: Operations such as addition, subtraction, multiplication and division can be done using a single command instead of multiple loops...

Plotting in MATLAB

The MATLAB graphics system consists of high-level commands for two-dimensional and three-dimensional data visualization, image processing, animation, and presentation graphics. It also includes low-level commands that allows to fully customize the appearance of graphics as well as to build complete Graphical User Interfaces. Given below is a code for plotting a Parabola:...

Contact Us