You are on page 1of 19

Structure of a Program

Matlab Basics
Content: Today we will see
 Some basic rules about Matlab coding
 The template of a Matlab program
 How to display text messages in Matlab
 Some common errors in Matlab coding
 Some good practices
My First Program
 Write a Matlab program that displays the
following message on the screen

Welcome to ENGR 113 !


My First Program

Welcome to ENGR 113 !


My First Program
 Rule: any text
message must
be enclosed in
single
quotation
marks
‘Welcome to ENGR 113 ! ‘
My First Program
 Rule: all matlab
commands
must be in
small letters

fprintf( ‘ Welcome to ENGR 112 !’)


My First Program
 fprintf is a
matlab
command
(function) that
prints a text
message
fprintf( ‘ Welcome to ENGR 112 !’) ;
My First Program
 Rule: In
preference
terminate each
statement in
matlab with a
semi-colon (;)
fprintf( ‘ Welcome to ENGR 112 !’) ;
 But there are
exceptions. We
will see them
later
Matlab program template: function
template
function and end
are part of Matlab
programming
language

The program must be saved in a file carrying the


same name than the function with extension .m
PrintHelloWorld.m
Matlab program template: function
template : how to run the program

In the command prompt window type the name of the file in which your
program is saved (without the extension) followed by semincoln. Them press
Enter
Matlab program template: script
template
=

The program contains only the instructions. To run the program, type the type the
name of the file in which your program is saved (without the extension) followed
by semincoln. Them press Enter
Matlab program template: script
template
=

\n is a special character that make a new line after printing the text that comes
before .
MATLAB comments
 Comments are explanatory notes
 Increase the program readability
 They are ignored by the compiler

 To include comments in a program type %.


Printing special characters
 Rule: all escape sequences must be within the quotations
Escape Description Example Output
Sequen
\n Newline. Position the screen cursor cout<<“Hello \n World”; Hello
to the beginning of the next line. World
\t Horizontal tab. Move the screen cout<<“Hello \t World”; Hello World
cursor to the next tap stop.
\r Carriage return. Position the screen cout<<“Hello \r Word”; Word
Hello
cursor to the beginning of the
current line.
World
\a Alert. Sound the system bell cout<<“Hello \a”; Beep
\\ Backslash. Used to print backslash cout<<“hello \\ World”; Hello \ World
character
\” Double quote. Used to print double cout<<“\”hello\” ”; “hello”
quote character
14
Program Errors: Find the errors in
this code
What makes a bad program?
 Writing Code without detailed analysis and
design

 Repeating trial and error without


understanding the problem

 Writing tricky and dirty programs


Exercise
 Write a Matlab code that displays your
name, surname as shown below:

Mohamed
Abdul-Aziz
Exercise: solution
 Analysis:
 Output : text showing Name and
Surname in separate lines
 Input: None
 Process: Display the text mentioned
above
Exercise: solution (cont)
 1st solution

2nd solution

You might also like