Entering Commands

This example shows how to enter basic commands in MATLAB.

To create and process variables, use the assignment operator '='.

disp('Assign value to variable m:')
disp('>> m = 2*1.5')
m = 2*1.5
Assign value to variable m:
>> m = 2*1.5

m =


A semicolon ';' to the end of a command suppresses the output.

disp('Add semicolon to suppress output:')
disp('>> m = 2*1.5;')
m = 2*1.5;
Add semicolon to suppress output:
>> m = 2*1.5;

To conditionally execute statements, use the 'if', 'elseif', and 'else' statements.

disp(' ')
disp('Check value of m:')
if m > 5
    disp('m is greater than 5')
elseif m == 5
    disp('m is equal to 5')
    disp('m is less than 5')
Check value of m:
m is less than 5

MATLAB contains built-in functions and constants, such as 'abs' (absolute value) and 'pi'.

disp(' ')
disp('Calculate sine of pi/2, and assign it to y:')
disp('>> y = sin(pi/2)')
y = sin(pi/2)
Calculate sine of pi/2, and assign it to y:
>> y = sin(pi/2)

y =


관련 항목