0% found this document useful (0 votes)
13 views5 pages

Exp 2

Uploaded by

noorispoor0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Exp 2

Uploaded by

noorispoor0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 2: Basic Commands of MATLAB

This experiment introduces fundamental MATLAB commands essential


for basic operations and programming. Understanding these commands will
help in efficiently using MATLAB for various computational tasks.

Displaying Information
disp
The disp command displays text or variables in the command window.
Example:
1 disp ( ’ Hello , MATLAB ! ’) ;

fprintf
The fprintf command provides a formatted display of text or variables.
Example:
1 name = ’ John ’;
2 age = 25;
3 fprintf ( ’ Name : % s \ nAge : % d \ n ’ , name , age ) ;

Managing the Workspace


clear
The clear command removes variables from the workspace.
Example:
1 clear ; % Deletes all variables from the workspace
2 clear x ; % Deletes the variable ’x ’ from the workspace

1
clc
The clc command clears the command window.
Example:
1 clc ;

whos
The whos command provides a detailed list of all variables in the workspace,
including their sizes and data types.
Example:
1 x = 10;
2 y = [1 , 2 , 3];
3 name = ’ John Doe ’;
4 whos ;

Taking User Input


input
The input command prompts the user to enter data.
Example:
1 name = input ( ’ Enter your name : ’ , ’s ’) ;

Formatting Output
format
The format command controls the display format of numeric values. Various
options include:

• format short: Displays numbers with 4 decimal places (default set-


ting).

• format long: Displays numbers with 15 decimal places.

• format short e: Displays numbers in short scientific notation with 4


decimal places.

2
• format long e: Displays numbers in long scientific notation with 15
decimal places.

• format bank: Displays numbers with 2 decimal places and commas as


thousands separators.

• format +: Displays positive numbers with a plus sign.

• format rat: Displays numbers as rational fractions.


Examples:
1 format short ;
2 disp ( pi ) ;
3
4 format long ;
5 disp ( pi ) ;

Conditional Statements
if, else, elseif
Conditional statements allow decision-making based on conditions.
Example:
1 x = 10;
2 if x > 0
3 disp ( ’x is positive ’) ;
4 elseif x < 0
5 disp ( ’x is negative ’) ;
6 else
7 disp ( ’x is zero ’) ;
8 end

Loop Structures
for
The for loop executes a series of statements a specific number of times.
Example:
1 for i = 1:5
2 disp ( i ) ;
3 end

3
while
The while loop executes as long as a condition is true.
Example:
1 i = 1;
2 while i <= 5
3 disp ( i ) ;
4 i = i + 1;
5 end

Comments
Single-line Comments
Use the % symbol to denote single-line comments.
Example:
1 % This is a single - line comment in MATLAB .
2 x = 10; % This assigns the value 10 to the variable x .

Multi-line Comments
Use %{ and %} to enclose multi-line comments.
Example:
1 %{
2 This is a multi - line comment in MATLAB .
3 It can span multiple lines .
4 x = 10;
5 y = 20;
6 %}

Creating Scripts for Conditional and Loop State-


ments
Instructions for Creating Scripts
To create a script file in MATLAB: 1. Open MATLAB and go to the Home
tab. 2. Click on New Script to open the MATLAB Editor. 3. Type the
code for your script in the editor. 4. Save the script with a .m extension,
e.g., myScript.m.

4
Example: Script with if-else Statement
Script Name: ifElseScript.m
1 x = 10;
2 if x > 0
3 disp ( ’x is positive ’) ;
4 elseif x < 0
5 disp ( ’x is negative ’) ;
6 else
7 disp ( ’x is zero ’) ;
8 end

Example: Script with for Loop


Script Name: forLoopScript.m
1 for i = 1:5
2 disp ( i ) ;
3 end

Example: Script with while Loop


Script Name: whileLoopScript.m
1 i = 1;
2 while i <= 5
3 disp ( i ) ;
4 i = i + 1;
5 end

You might also like