Introduction To Octave

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Introduction to Octave

Overview
• Octave is the “open-source” of Matlab
• Octave is a great gnuplot wrapper (Interpreter)
– www.octave.org
– www.mathworks.com
• Octave and Matlab are both, high-level
languages and mathematical programming
environments for:
– Visualization, Programming, algorithm
development, Numerical computation: linear
algebra, optimization, control, statistics, signal
and image processing, etc
GUI 1
Current Directory

2
File Browser

Command Window
3

Variable Editor
4 5 6
Workspace Editor
7 Command History
Current Directory
• The current directory is the directory on
the computer where Octave script files will
be saved. The Current Directory textbox
displays the current directory on the
computer.
File Browser
• The file browser displays the directories
and files in the current directory.
Command Window
• The Command Window is the window
where Octave commands are executed
and the execution results are displayed to
the user. The command prompt symbol is
denoted by >>.
Workspace
• The Workspace window displays all the
variables that are currently being used by
the Octave. Details like the variable name,
dimension, value, etc are displayed.
Command History
• The Command History window displays
the previous Octave commands entered in
the Command Window.
Editor
• The Editor window is where we can enter
the code, save script files, execute, debug
and run the Octave scripts.
Tabs

File

Edit Debug
Tabs

• Tools Window Help News


Variable Naming Conventions
• A variable can have a short name (like x and y)
or a more descriptive name (age, carname,
total_volume). Rules for Octave variables:
• The name of a variable must be a sequence of
letters, digits and underscores, but it may not
begin with a digit
• Octave does not enforce a limit on the length of
variable names, but it is seldom useful to have
variables with names longer than about 30
characters
Examples
• x
• x15
• __foo_bar_baz__
• fucnrdthsucngtagdjb
Variable Naming Conventions
• Case is significant in variable names. The
symbols a and A are distinct variables.
• Built-in variable
– ans
• Built-in function
– Create valid unique variable name(s) from
string(str)
– Ex. genvarname ({“Elective3”,”Block2”})
Variable Naming Conventions
• Function file: namelengthmax()
• Strings:
– str = “Hello”
– str1 = ‘Hello World’
Escape Sequences in String Constants

• In double-quoted strings, the backslash character is


used to introduce escape sequences that represent
other characters. For example, ‘\n’ embeds a newline
character in a double-quoted string and ‘\"’ embeds a
double quote character. In single-quoted strings,
backslash is not a special character. Here is an example
showing the difference:
double ("\n")
ans = 10
double ('\n’)
ans = [ 92 110 ]
Here is a table of all the escape sequences used in Octave
(within double quoted strings).
Symbol Description
\\ Represents a literal backslash, ‘\’.
\" Represents a literal double-quote character, ‘"’.
\' Represents a literal single-quote character, ‘'’.
\0 Represents the null character, control-@, ASCII code 0.
\a Represents the “alert” character, control-g, ASCII code 7.
\b Represents a backspace, control-h, ASCII code 8.
\f Represents a form feed, control-l, ASCII code 12.
\n Represents a newline, control-j, ASCII code 10.
\r Represents a carriage return, control-m, ASCII code 13.
\t Represents a horizontal tab, control-i, ASCII code 9.
\v Represents a vertical tab, control-k, ASCII code 11.
\nnn Represents the octal value nnn, where nnn are one to three digits between
0
and 7. For example, the code for the ASCII ESC (escape) character is ‘\
033’.
\xhh Represents the hexadecimal value hh, where hh are hexadecimal digits (‘0’
through ‘9’ and either ‘A’ through ‘F’ or ‘a’ through ‘f’).
Variable Naming Conventions
• Variables and Data Types
– Matrices (real and complex)
– Strings (matrices of characters)
– Structures
• Vectors - It's a matrix with one column/row
• Scalars - It's a matrix of dimension 1x1
• Integers - It's a double
• Boolean - It's an integer (non-null=true,
0=false)
Variables and Data Types
• Creating Matrix
– A = [1,3,5]
• Matrix Arithmetic (Addition, Subtraction,
Multiplication, Division)
• Create a string array from a numeric
matrix
– setstr ([97, 98, 99])
ans = abc
String Conversions
• Function file: bin2dec(string)
• Function file: hex2dec(number)
• Function file: dec2hex(number)
Operations and Expressions
• Arithmetic
Name Example
Addition x+y
Subtraction x-y
Multiplication x*y
Division x/y
Power operator x**y
Transpose
x. ’
Negation -x
Comparison Operators
• Comparison operators compare numeric
values for relationships such as equality.
They are written using relational operators.
• All of Octave’s comparison operators return
a value of 1 if the comparison is true, or 0 if
it is false. For matrix values, they all work
on an element-by-element basis.
• +
Comparison Operators
Example Rule
x<y True if x is less than y
x <= y True if x is less than or equal to y
x == y True if x is equal to y
x >= y True if x is greater than or equal to y
x>y True if x is greater than y
x != y
x ~= y True if x is not equal to y
Control Statements
• The if statement
if (condition)
then-body 1st form
endif
if (condition)
then-body
else 2nd form
else-body
endif
Activity
• In the Editor, create a string of your full
name and save it as <fullname.m>
• Save the file to your GitHub repository
named as: Elective3-2
• Hint: Use the ASCII table to view the dec
equivalence of each ASCII character

You might also like