Octave showing the perfect compatibility with MATLAB keeps its tag of being called open source clone of MATLAB. Learning one paves the way to learn another without any additional effort. The article titled “Octave:1> Getting Started” discusses the desktop environment and develops some familiarities with GNU Octave. This article is a continuation of the series dealing with variables and constants in Octave. Like the earlier article, we encourage readers planning to learn octave/Matlab to try the exercises marked as “Ex“.
Variables
To store a value in an Octave session, or in a program, a variable is used like any other programming language. Although it is not necessary to define the type of variable. Octave can itself set a type based on assignment expression. Once the variable is created, Workspace Window shows variables along with their values, type and size of the container. One easy way to create a variable is to use an assignment statement.
variablename = expression
The variable is always on the left, followed by the “=” symbol, which is the assignment operator. after the assignment, the command window immediately shows the result. To suppress this result output a semicolon can be used. This will save the variable value in memory but doesn’t display it immediately.
Ex: Try typing "mynum = 6" , "mynum = 6;" and "6 = mynum" in command window. Also try to understand the error displayed in last case.
If no variable is assigned in left a generic variable “ans” is used to store the result.
>> 9 - 2
ans = 7
To change a variable, another assignment statement can be used, which assigns the value of a different expression to it.
Ex: Try typing "mynum = 6" and observe workspace window. Now try "mynum = 'c'" and see the outcome of workspace window.
Naming Conventions
Variable names are examples of identifier names. So the rules of naming an identifier apply to a variable name.
- The name must begin with a letter of the alphabet.
- Name can contain letters, digits, and the underscore character (When run in MATLAB underscore might give issues, hence it is a better practice).
- it cannot have space in between.
- Name is case sensitive, i.e. “var1”, “VAR1” and “Var1” represent seperate variables.
- Reserved words or keywords cannot be used as variable names (Names of built-in functions can, but should not, be used as variable names).
- Variable name should be less than “namelengthmax“.
Ex: Try typing "namelengthmax" on command prompt.
Data types in Octave
Every variable has a data type associated with it, which are defined by classes. A class is a combination of a type and the valid operations that can be performed on values of that type. The standard built-in types are real and complex scalars and matrices, ranges, character string a data structure type, and a cell array. All the variables fall under one of the following types:
- “double“: double-precision IEEE Standard 754 Floating Point (8 bytes)
- “single“: single-precision IEEE Standard 754 Floating Point (4 bytes)
- “logical” : true/false (1 byte)
- “char” : character e.g. ‘c’ (1 byte)
- “int8“: signed integer 8 bit (1 byte), range [-128, 127]
- “int16” : signed integer 16 bit (2 bytes) , range [-32768, 32767]
- “int32” : signed integer 32 bit (4 bytes) , range [-2147483648, 2147483647]
- “int64“: signed integer 64 bit (8 bytes), range [-9223372036854775808, 9223372036854775807]
- “uint8” : unsigned integer 8 bit (1 byte), range [0, 255]
- “uint16” : unsigned integer 16 bit (2 bytes), range [0, 65535]
- “uint32” : unsigned integer 32 bit (4 bytes), range [0, 4294967295]
- “uint64“: unsigned integer 64 bit (8 bytes), range [0, 18446744073709551615]
A complex number is stored as an array of two real values. i.e. two more derived data types can be “double complex“, “single complex“.
Data Containers
Octave has two different mechanisms to contain arbitrary data types in the same variable.
- C-like Structures, which are indexed with named fields.
>> x.a = 1;
>> x.b = 2;
>> x
x =
scalar structure containing the fields:
a = 1
b = 2
- And Python list like cell arrays, where each element of the array can have a different data type and or shape. Cell arrays work just like N-dimensional arrays with the exception of the use of ‘{’ and ‘}’ as allocation and indexing operators.
>> y = {1, "string"}
y =
{
[1,1] = 1
[1,2] = string
}
Multiple input arguments and return values of functions are organized as another data container, the comma-separated list. Comma-separated lists are the basic argument type to all Octave functions – both for input and return arguments. It can appear on both the right and left-hand side of an assignment.
Ex: Try creating structure and cell type data container in octave and observe their properties in Workspace Window.
Constants
Variables are used to store values that might change, or for which the values are not known ahead of time. Octave has the capacity to store constants, which are values that are known ahead of time and cannot possibly change. An example of a constant value would be pi with value 3.1416.
>> i
ans = 0 + 0i
>> j
ans = 0 + 0i
>> e
ans = 2.7183
Similarly, “inf” stands for infinity and “NaN” stands for not a number.
Selfhelp
- “who” shows variables that have been defined in Command Window.
- “whos” show variables with attributes and properties that have been defined in Command Window.
- “clear” command clears out all declared variables. It can be used with variable names as an argument to clear specific variables only.
- “intmin” or “intmax” command with integer type as string input gives the minimum and maximum possible value of that integer data type, e.g. “intmin (‘int8’)“.
Ex: try out the above mentioned commands on your own.
- GNU Octave – Scientific Programming Language
- Octave:1> Getting Started
- Octave:1> Variables and Constants
- Octave:1> Expressions and operators
- Octave:1> Built-In Functions, Commands, and Package
- Octave:2> Vectors and Matrices Basics
- Octave:2> Scripts and Algorithm
- Octave:2> Conditional Execution
- Octave:2> Functions
- Octave:3> File Operations
- Octave:3> Data Visualization