0% found this document useful (0 votes)
47 views

MATLAB Scripts and Programming: Dr. Ugur GUVEN

This document discusses creating and using scripts in MATLAB. It begins by explaining how to write scripts, use comments, and get user input. It then provides a simple example script that draws a circle based on a user-input radius. Finally, it outlines various programming commands in MATLAB like for loops, while loops, and if/elseif/else statements to control program flow, along with break and error commands.
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)
47 views

MATLAB Scripts and Programming: Dr. Ugur GUVEN

This document discusses creating and using scripts in MATLAB. It begins by explaining how to write scripts, use comments, and get user input. It then provides a simple example script that draws a circle based on a user-input radius. Finally, it outlines various programming commands in MATLAB like for loops, while loops, and if/elseif/else statements to control program flow, along with break and error commands.
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/ 7

MATLAB Scripts and

Programming
Dr. Ugur GUVEN

Creating Script Files in MATLAB


Just like a programming language, you can
write scripts (mini programs) in MATLAB. This
way instead of typing every command in the
prompt, you can automate your calculations
% is used to create a non-executed remark in
the script
Select New M-File from the menu
You can ask the user to input a value to
MATLAB by using the input command
x=input(Enter the required value: )

Simple Script to Draw a Circle in


MATLAB
% This is a program to draw a Circle
% (;) is used so that MATLAB executes all at once
% User inputs the radius of the circle
r=input(Enter the radius of the circle: )
theta=linspace(0,2*pi,100);
% Create vector
x=r*cos(theta);
% generate x
y=r* sin(theta);
% generate y
plot (x,y);
% plot the circle
axis(equal);
% equalize axis
title(circle of given radius r) % graph title

Programming Commands in MATLAB


For Loop
For loop is used to repeat a statement or a group of
statements for a fixed number of times
The syntax is for for i=m,k,n where counter i is
advanced by k until n is reached
for m=1:100
num = 1/(m+1)
end
for n=100:-2:0
k=1/(exp(n))
end

Programming Commands in MATLAB


While Loop

While loop is used to execute a statement or a


group of statements for an indefinite number of
times until the condition specified by while is no
longer valid
% Finds the powers of 2 below 10000
v=1;
num=1;
i=1
while num < 10000
v=[v;num];
i= i+1;
num=2^i;
end
v %display v

Programming Commands in MATLAB


If-Elseif-Else Statement
If-Elseif-Else statement is used to test logical conditions
in the scripts. You can have numerous If statements. If
you want you can have a single if statement, but end
statement must be present
i=6; j=21;
if i > 5
k =i
elseif (i>1) & (j==20)
k = 5*i;
else
k=1;
end

Programming Commands in MATLAB


There are numerous MATLAB commands that
you can use in a script. You should use Help
menu to see them all
break statement terminates any loop
regardless of the condition of the loop
The command error(message) inside a
function or a script aborts the execution and
displays the error message and returns
command to keyboard

You might also like