MATLAB Basics

Create a variable named a by typing this statement at the command line

>> a = 1

a =

1

Hide Result by adding (;) at end of statement.

>> a = 1;

When you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store the results of your calculation.

>> sin(a)

ans =

0.8415

Built-in Functions:

>> d = cos(a)

d =

0.5403

sin

Sine of argument in radians

sind

Sine of argument in degrees

asin

Inverse sine in radians

asind

Inverse sine in degrees

sinh

Hyperbolic sine of argument in radians

asinh

Inverse hyperbolic sine

cos

Cosine of argument in radians

cosd

Cosine of argument in degrees

acos

Inverse cosine in radians

acosd

Inverse cosine in degrees

cosh

Hyperbolic cosine

acosh

Inverse hyperbolic cosine

tan

Tangent of argument in radians

tand

Tangent of argument in degrees

atan

Inverse tangent in radians

atand

Inverse tangent in degrees

atan2

Four-quadrant inverse tangent

 

Square root:

>> sqrt(36)

ans =

6

Complex Numbers

Complex numbers have both real and imaginary parts, where the imaginary unit is the square root of -1.

To represent the imaginary part of complex numbers, use either i or j .

>> sqrt(-1)

ans =

   0.0000 + 1.0000i

 

Workspace Variables

- MATLAB Is case-sensitive

The workspace contains variables that you create within or import into MATLAB from data files or other programs. For example, these statements create variables A and B in the workspace.

>> A = 9;

>> B = 10;

Workspace variables do not persist after you exit MATLAB. Save your data for later use with the save command.

   >> save C:\myfile.mat

 

Saving preserves the workspace in your current working folder in a compressed file with a .mat extension, called a MAT-file.

 

To clear all the variables from the workspace, use the clear command.

>> clear

 

Restore data from a MAT-file into the workspace using load.

>> load C:\myfile.mat

 

Text and Characters:

When you are working with text, enclose sequences of characters in single quotes. You can assign text to a variable.

>> myText = 'Hello, world';

 

  • If the text includes a single quote, use two single quotes within the definition.

>> otherText = 'You''re right'

otherText =

'You're right'

You can concatenate character arrays with square brackets

>> longText = [myText,' - ',otherText]

longText =

'Hello, world - You're right'

To convert numeric values to characters, use functions, such as num2str or int2str.

>> f = 71;

>> c = (f-32)/1.8;

>> tempText = ['Temperature is ',num2str(c),'C']

tempText =

'Temperature is 21.6667C'

 

  • Factorial of input

>> f = factorial(9)

f =

362880

  • Absolute value and complex magnitude

>> y = abs(-9);

  • Quotient of integer division

>> floor(23/5);

  • remainder

>> rem(23,5);

>> mod(23,5);

  • Integer Part

>> fix (23/5);

  • Real nth root of real numbers

>> Y = nthroot(16,4);

  • Is Prime numbers

>> isprime (5);

>> factor (30);

 

 

 

  • Convert to Base System

base2dec

Convert base N number string to decimal number

bin2dec

Convert binary number string to decimal number

dec2base

Convert decimal to base N number in string

dec2bin

Convert decimal to binary number in string

dec2hex

Convert decimal to hexadecimal number in string

hex2dec

Convert hexadecimal number string to decimal number

hex2num

Convert hexadecimal number string to double-precision number

num2hex

Convert singles and doubles to IEEE hexadecimal strings