 ## What is Octave?

OCTAVE is a high–level language designed for scientific computations. It is most compatible with MATLAB in speed and syntax (to some extent).

Octave can be downloaded from https://www.gnu.org/software/octave/download.html#ms-windows. It features a simple-to-use GUI for scripting files and a command line for instant execution of a line of code. Since it is based on MATLAB, Octave produces M-files, i.e., .m extension files.

It is free open source software under GNU General Public License. It was made for computing linear algebra and matrices. Now, it is used for making machine learning models.

## Advantages of Octave over others

There are many other languages for making machine learning models in the market, like Python, Ruby, etc. with many with built-in functions which makes them easier to use. But, one cannot understand the spices inside the functions. Using Octave, we make those spices and can manipulate it as per the requirements.

There are many other advantages of using octave:

1. It has C, C++ and FORTRAN as its base languages, which makes it a lot faster.
2. Its syntax is very close to mathematical expressions which makes it a good choice for engineers.
3. It has high level plot commands for 2D and 3D, for data visualization.
4. It supports contour plots.

3. Having the process done, install it by following the images (if required).

4. Octave is installed. Now open the Octave CLI.

“Now you are all set to get started.”

In the tutorial, you will get many examples. So, it is advised to practice those by, instead of just copy-paste, typing each letter and try modifying the same. By making mistakes you will get to know many new things. Hope, you will enjoy the tutorial!

### Useful Things To Know

It is always better to know about a place before you visit. It helps you not get into trouble or helps you get out of it.

• So, in Octave, maybe the execution of your results into an infinite iteration then you can type ctrl + C to terminate the process. You can terminate any process with this shortcut.
• One-line commenting in Octave either using # or %.

>> % I am a one-line comment

>>

• Block commenting in Octave : #{ …. #} or %{ … %}.
• A line of code is preferred to end with semicolon ‘;’ to stop the instant execution.
• There is one more command that will help you to understand Octave. It is about taking help from Octave by typing help or doc followed by a command about which you wanted to know. This command opens the documentation of the command in the command window or command-line interface (CLI). For instance,
```>>  help clc                             % CLC is a command in Octave
'clc' is a built-in function from the file libinterp/corefcn/sysdep.cc
-- clc ()
-- home ()
Clear the terminal screen and move the cursor to the upper left corner.
. . .

```

You can try these commands: help help, help plot, help isvarname, help size, help inv, help pinv, help reshape,     help resize, help round, help cast, help isa, help int8, help logical

Octave GUI has a documentation window as well where you can find many functions and commands.

To close Octave, type exit or quit.

## VariablesandDatatypes

Variables are x and y that we have been using since our junior classes. We store values in variables and the type of the value stored is called the Data type of the variable.

Like other programming languages, Octave also provides a simple way to declare variables with the following conditions:

1. It must be a sequence of letters, digits and underscores, not beginning from digits. So, my_var12, my21var_, _var12 and _123var are valid variables but 21my_var is an invalid variables.

2. It should be kept in mind that the Octave is case-sensitive. So, myVar and myvar are treated as different variables.

3. To check the validity of a variable name, there is a function to use: isvarname (‘variable_name’). This returns Boolean value, i.e., true (1) or false (0).

4. Octave has an automatic variable: ans,which appears on pressing “Enter”after writing a line of code.

Octave has some standard built-in data types for real and complex scalars, matrices, ranges, characters, a data structure type, and cell arrays. For example, “scalar”, “matrix”, etc. The function typeinfo (expr) returns the data type of the expr in string form.

As said earlier, Octave works very fast with vectors and matrices. Let’s understand some small but useful terms.

Matrices are a collection of entities in some m rows and n columns. Vectors or arrays are matrices with one column or row. Scalars are matrices with dimension 1 x 1, i.e., a scalar is a single entity matrix. The entities are categorized into integers, characters and logical. Integers are set to “double” by default which has the largest range of numbers. Other values for integers are “int8”, “int16”, “int32”, “int64”, “uint8”, “uint16”, “uint32”, “uint64” and “single”. Characters are single/double quoted characters like ‘ab’, ‘cdac’, ‘1’, etc. while logicals are binary values, i.e., 0 (false) and 1 (true)

To create a matrix, simply type:

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5     4    -3
7    -6     4
9     1     3
>>

```

and Octave responses in clear matrix form.

To create a vector, we just need to replace semicolon inside square braces with spaces.

```>> V = [5 4 -3 7 -6 4 9 1 3]
V   =    5     4    -3     7    -6     4     9     1     3

```

To create a character string, simply type:

```>> myStr = ‘I am a string.’
myStr = I am a string.

```

Unlike MATLAB, Octave also works the same with double quotes.

To display a declared variable, just type the variable name:

```>> a = 23              % a is declared and the value 23 is assigned to it
a   =   23

```

To stop the instant output, semicolon ‘;’ is used at the end of the line:

```>> a;
>> sin(pi);
>>

```

But, if we don’t want to display the name of the variable, then wrap the variable name in disp () function.

```>> disp (a)      % a is declared and the value 23 is assigned to it
23

```

The value and data type of a variable are inconstant, i.e, it can be changed whenever required.

```>> myVar = 21
myVar   =   21
>> myVar = 23
myVar   =   23
>> disp (myVar)
23

```

There are some tricks to know in Octave.

• Command who is used to list the currently defined variables while the command whos gives more detailed list with name, size and class columns.
```>> who
A      V      a      myStr      myVar
>>

```
• We can type the previously typed code by typing the first few letters and scrolling up. It lessens our effort.
• Also, we can scroll up to auto write the earlier codes.
• There are some variables which are mathematical constants used for mathematical uses. Some of those are pi, e, realmin, realmax, eps, I, etc.
```>> disp (pi)                  % displays the value of pi without writing ‘pi’
3.1416
>> disp (e)
2.7183
>> disp (realmin)
2.2251e-308
>> disp (realmax)
1.7977e+308
>> disp (eps)
2.2204e-16
>> disp(I)
0 + 1i
>>

```

## Functions

Functions are like a device which takes some input, processes it and produces output, based on the given input.

We had gone through many functions like isvariable() which takes a variable name and checks it for the validity conditions and finally returns the output. Other functions were size () returns the dimensions of the inputted matrix; strcat ()returns the inputted strings in the same order after concatenation; inv () returns the inverse of a non-singular matrix.

So, there are many built-in functions in Octave to use. But, for the sake of flexibility, in Octave we can define our function as well. For this, we need to create a .m file with the function name as the filename. For instace, let we want to create sum2n () which basically takes one argument and from 1 sums to it, i.e., sum2n (3) should return 6.

Lets create it in the Ocatve’s editor window.

```function sum = sum2n (n)
sum = n * (n + 1) / 2;
endfunction;

```

The ‘sum’ after function is a variable which will be returned when sum2n (…) is executed.

Let’s see another example.

```function [mn mx] = limits (v)
% The function limits take a vector v and simply returns the minimum value and maximum value in the vector

mn = min (v); mx = max (v);
endfunction;

```

Now, try it in the command window as an exercise. (Hint: You need to assign the function to an 1×2 vector.)

There is another way to create a function instantly. This function is called an anonymous function. Its declaration doesn’t follow the systematic procedure. Also, the anonymous function can be installed into a variable. For the declaration of an anonymous function, the syntax is @ (arguments…) (function…).

```>> @ sin (0)       % creates a sine function and instantly feed inside it 0
ans   =   0
>> f = @ (x) (x*log(x +1))      % the function x*(log(x + 1)) dependent of x installed to variable ‘f’
f   =
@(x) (x * log (x + 1))
>> f(1)         % displays the output of function f of 1
ans   =   0.69315
>>

```

There are many cases when we need to create a function inside a function, in that case as well we use anonymous functions.

It is obvious that the variables declared inside the function are local variables and cannot be accessed from outside the function. However, local variables can be accessed outside the function using the global keyword.

Try searching these functions: any() and all().

## Matrices

The base of creating a matrix is illustrated earlier as:

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5     4    -3
7    -6     4
9     1     3
>> A = [5, 4, -3; 7, -6, 4; 9, 1, 3]
A   =
5     4    -3
7    -6     4
9     1     3
>>

```

As clearly visible, elements in a row are differentiated by a space or ‘,’.

We have some special matrices in Octave. Some are zero matrix, identity matrix, and diagonal matrix.

```>> zeros (2, 3)          % returns a 2 by 3 matrix having all 0 elements
ans   =
0     0     0
0     0     0
>> ones (3,2)            % returns a 3 by 2 matrix having all 1 elements
ans   =
1
1     1
1     1
>> eye (3)                 % returns an identity matrix of order 3
ans   =
1     0     0
0     1     0
0     0     1
>> diag ([1 2 3])       % returns a diagonal matrix of order 3 with 1, 2, 3 as diagonal elements
ans   =
1     0     0
0     2     0
0     0     3
>>

```

Try magic (4) !

Also, we can install functions in place of numbers. For instance,

```>> A = [sin(pi)  tan(pi/4)  cos(pi/2); acos(-1)  atan(1)  tan(pi/2);  cos(2*pi)  sec(2*pi)  log(23)]
A =
1.2246e-16   1.0000e+00   6.1230e-17
3.1416e+00   7.8540e-01   1.6331e+16
1.0000e+00   1.0000e+00   3.1355e+00
>>

```

Let’s look at some useful matrix functions.

To add a column and row to a matrix:

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5     4    -3
7    -6     4
9     1     3
>> [A, [11; 22; 33]]               % Adds a column to matrix A
ans   =
5     4    -3     11
7    -6     4     22
9     1     3     33
>> [A; [11 22 33]]               % Adds a row to matrix A
ans   =
5     4    -3
7    -6     4
9     1     3
11  22   33
>>

```

Similarly, more columns and rows can be added.

Remember, indexing in Octave starts from ‘1’ and when extracting elements, always ‘row comes before columns’ followed by further dimensions.

To extract elements from a matrix, syntax matrix_name (index1, index2, …) is used.

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5     4    -3
7    -6     4
9     1     3
>> disp (A(1)); disp (A(2)); disp (2, 1)
5
4
4
>> disp (A(2,3))         % display the element at 2nd row and 3rd column of matrix A
4

```

From the second code in the above example, A(m, n) is the element at mth row and nth column.

To extract all elements from a dimension, row or column, colon ‘:’ is used.

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5     4    -3
7    -6     4
9     1     3
>> A(1, : )          % all elements from 1st row
ans   =
5     4    -3
>> A(:, 2)          % all elements from 2nd column
ans   =
4
-6
1
>>

```

We can extract a matrix (collections of rows or columns) using colon ‘:’ to delimit the range.

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5     4     -3
7     -6     4
9     1     3
>> A(2:3, : )          % matrix  all elements from 2nd to 3rd rows
ans   =
7     -6    4
9     1     3
>> A(:, 1:2)         % matrix of all elements from 1st to 2nd columns
ans   =
5     4
7    -6
9    1
>> A(2:3, 1:2)    % matrix of all elements from 2nd to 3rd rows and 1st to 2nd columns
ans   =
7    -6
9     1
>> A(:, 2:end)    % end keyword extracts to the last index
ans   =
7    -6     4
9     1    3
>>

```

In almost every case m:n means from m to n.

Defining a range is very easy in Ocatve. It requires syntax start_num : increment(optional) : end_num.

```>> range = 1:10           % an array of elements from 1 to 10 is installed in range, i.e., a row vector of 10 elements
range   =
1     2     3     4     5     6     7     8     9     10
>> range2 = 1:2:10
Range2   =
1     3     5     7     9
>>

```

Another way for creating a range is linspace () command with syntax linspace (start_num, end_num, no_of_elements).

```>> newRange = linspace (1: 10:10)         % creates a row vector with 10 elements starting from 1 to 10
newRange   =
1     2     3     4     5     6     7     8     9     10
>>

```

It should be noted that the no_of_elements is not a natural number, a vector with only element 0 is produced.

```>> emptyV = linspace (1: 10: 0.1)
emptyV   =
[] (1x0)
>> emptyV2 = linspace (1: 10: 0)
emptyV2   =
[] (1x0)
>>

```

We can assign a value to an element, a row or a column using an assignment operator ‘=’.

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5      4     -3
7     -6      4
9      1      3
>> A(1, 2) = -4       % the element at 1st row and 2nd  is assigned to -4
A   =
5     -4     -3
7     -6      4
9      1      3
>> A(:, 2) = [4; 6; -1]       % the 2nd column is assigned
A   =
5     -4     -3
7      6     4
9     -1     3
>> A(3, : ) = [-9 1 -3]       % the 3rd row is assigned
A   =
5      4     -3
7     -6     4
-9     1    -3
>>

```

To delete a row or column just assign it an empty matrix [].

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5      4     -3
7     -6      4
9      1      3
>> A(2,: ) = []          % the 2nd row is assigned the empty matrix and is deleted
A   =
5     4    -3
9     1     3
>> newA = [A; [2 4 5; 3 7 1]]
newA   =
5      4    -3
7    -6     4
9      1     3
2     4     5
3     7     1
>> newA(1:2:5, : ) = []
newA   =
7    -6     4
2     4     5
>>

```

We can know about our matrix through the following commands:

```>> A = [5 4 -3; 7 -6 4; 9 1 3; 2 4 5; 3 7 1]
A   =
5      4    -3
7    -6     4
9      1     3
2     4     5
3     7     1
>> size (A)         % returns an two element row vector having no of rows followed by no of columns
ans   =
5     3
>> no_of_rows = size (A,1)      % returns no of rows
no_of_rows   =   5
>> no_of_columns = size (A,2)      % returns no of columns
no_of_columns   =   3
>> rows (A)      % equivalent to size (A,1)
ans   =   5
>> columns (A)   % equivalent to size (A,2)
ans   =   3
>> [m n] = size (A)       % m => no of rows &amp; n => no of columns (good practice)
m   =   5
n   =   3
>> length (A)         % returns the max of size (A)
ans   =   5
>> numel (A)         % returns the total no of elements in A
ans   =   15
>> isempty (A)      % returns logical if A is empty(1) or not (0)
ans   =   0
>> ismatrix (A)     % checks for matrix, similar is isvector()
ans   =   1
>>

```

Now, let’s look into some of the matrix operations:

Transpose of a matrix is a matrix produced by interchanging rows and columns. In Octave, the transpose of A is A’.

```>> A = [5 4 -3; 7 -6 4; 9 1 3; 2 4 5; 3 7 1]
A   =
5      4    -3
7    -6     4
9      1     3
2     4     5
3     7     1
>> A’          % also transpose (A)
ans   =
5   7   9   2   3
4  -6   1   4   7
-3   4   3   5   1
>>

```

Determinant (|A|) is a unique number associated with a square matrix (matrix with equal no of rows and columns). We calculate it using det() function.

```>> A = [5 4 -3; 7 -6 4; 9 1 3];
>> det (A)         % returns the determinant of A
ans   =   -233
>>

```

Inverse of a matrix A (A-1) is a matrix such that the matrix multiplication of A and its inverse results into an identity matrix I, i.e., A*A-1 = I. For a matrix to be invertible, it must be singular, i.e, |A| ≠ 0. In Octave, inverse of A is inv (A).

```>> A = [5 4 -3; 7 -6 4; 9 1 3]
A   =
5     4    -3
7    -6     4
9     1     3
>> inv (A)        % returns the inverse of A
ans =
0.0944206   0.0643777   0.0085837
-0.0643777  -0.1802575   0.1759657
-0.2618026  -0.1330472   0.2489270
>>

```

However, for non-singular square matrices, there is a pseudo-inverse function pinv () to calculate the inverse.

## Operators

Like other programming languages, octave also has operators. Let’s see some of them.

• Arithmetic Operators

These operators are used for simple mathematical operations.

Octave supports ‘.’ operator for element-wise operations in case of matrices.

For instance, consider a matrix A = [1  2  3; 4  5  6; 7  8  9]

In Octave, matrix multiplication 🡪 A^2 = [30  36  42;  66  81  96;  102  126  150]

And, element-wise multiplication 🡪 A.^2 = [1  4  9;  16  25  36;  49  64  81]

• Relational Operators

These are binary operators and returns only 0 and 1 if the result of the operation is false and true respectively.

These operators work on both scalar and non-scalar entities. With arrays, it performs element-wise operations.

• Logical Operators

These operators take 0s and 1s as arguments and return the binary numbers. Its syntax is :

(expr1) logical_operator (expr2)

For non-scalar exprs’, logical operators returns an of binary numbers.

Instead of logical operators, there are three functions or (x1, x2, …), and (x1, x2, …), and not (x1).

• Other Operators

## Strings

In Octave, a string is treated as an array of characters. For instance,

```>> myStr = ‘I am a string.’
myStr   =   I am a string
>> whos
Attr  Name        Size                       Bytes  Class
==== ====        ====                     =====  =====
m              1x14                           14  char
>> % Some string operations
>> myStr (4)          % returns the character at index 4
ans   =   m
>> myStr (3:10)     % returns the characters from index 3 to 10 in a sequence
ans   =   am a str
>> myStr (1: 2: 12)     % returns the characters from index 1 to 12 stepping to second character after each character
ans   =    Ia  ti
>> c = 21; int2str (c)             % the function converts integer (21) to string
ans   =   21
>> d = 23.98674; num2int (d)   % the function converts the number to string
ans   =   23.98674
>> num2int (d, 5)           % the function converts the number to string and prints first 5 characters
ans   =   23.986
>> strcat (mystr(1: 7), ‘concatenated’, mystr(7:end))
ans   =   I am a concatenated string
>>

```

There are more functions that you can search in Octave using the help command. Try searching these functions:

strjust(), str2num(), tolower(), toupper().

## Programming

So far, we have been seeing Octave as a calculator. Now, we will try to use it as a programming language using some decision making statements, for and while loops, etc.

#### Decision-Making Statements

If statement executes the code inside it if the condition given to it is true.

```>> age = 14;
>> if  (age < 16),
disp(‘You are below 16 yrs.’);
endif;
You are below 16 yrs.
>>

```

If-elseif statement executes the code instantly if the condition above it is true.

```>> age = 14;
>> if (age < 12),
disp(‘You are under 12’);
elseif (age < 18),
disp(‘You are a teen.’);
else
disp(‘You are above 18.’)
end
You are a teen.
>>

```

*There is no limit in using elseif conditions.

Switch statement is case-specific, i.e., only that line of code will be executed whose case label satisfies the switch condition.

```>> age = 12;
>> switch (age),       % age is set to 12
case 5
disp(‘You are 5 yrs old.’);
case 12
disp(‘You are 12 yrs old.’);
case 18
disp(‘You are 18 yrs old.’)
otherwise
end
You are 12 yrs old.
>>

```

#### Iterative Loops

do-until loop iterates until the until condition not fulfilled. Its syntax is do,  body; until (condition) which seems really easy to understand like ‘iterate the body till the until condition is not reached’. In the below example, we will create the loop to get the factorial of a number.

```>> fact = 1; i = 1;       % variables fact and i is initialized to 1
>> do        % initializes the do-until loop
i++;         % increases the variable I by unity until it fulfills the until condition
fact *= i;      % similar to fact = fact * i ; excutes until i fulfills the until condition
until (i == 5)     % ends the do-until loop after declaring the until condition.
>> fact        % installs the current value of ‘fact’ to the Octave variable ans
ans   =   120
>>

```

while statement iterates till the conditions holds true. For example,

```>> i = 0;       % variable i is initialized to 0
>> while i<10,       % initializes the while loop by declaring while-condition
i++;         % increments i each time till it fulfils the while-condition
i         % prints the value in i
endwhile
i   =   1
i   =   2
i   =   3
i   =   4
>>

```

for statement iterates the code untill the expression completes. For example,

```>> for i = 1:10,      % initializes the for loop and the variable i, feed it value from 1 to 10 in each iteration
i            % prints  i in each iteration
endfor
i   =   2
i   =   3
i   =   4
i   =   5
i   =   6
i   =   7
i   =   8
i   =   9
i   =   10
i   =   11
>>

```

To interrupt a loop, break keyword to get out of the loop (for or while) while to skip the rest of the body continue keyword is used.

In speed, the for and while loops are almost the same. So, use that one which is easier to use.

To know the time taken by Octave to execute the whole loop, use tictac command. Write tic above and tac below the loop. For instance,

```>> i = 0;       % variable i is initialized to 0
>> tic, while i<10,       % tic initializes the time to 0 seconds
i++;         % increments i each time till it fulfils the while-condition
i         % prints the value in i
endwhile, toc       % toc returns the time taken to execute the while loop from time = 0 seconds
Elapsed time is 1.7881e-05 seconds.
>>

```

Note, the time shown to me may vary to you for the same code. It time it outputs is based on your system.

## Plotting

Octave provides an interactive graph. This graph is invoked using plot() function which requires two arrays as arguments. For instance,

```>> x = -10:0.1:10;
>> plot(x, sin (x))        % plots the ‘x’ in x-axis and sin of x in y-axis
>>

```

The execution invokes the following figure:

Some useful commands in Octave for plottings:

Let’s use the above commands to plot a meaningful sin function.

```>> x = -10:0.1:10;    % creates a vector with numbers from -10 to 10 with difference 0.1
>> y = sin (x);         % creates a vector containing the sin of each number in ‘x’
>> plot (x, y);         % creates a figure showing the x-y graph and displays the below image
>> grid on;           % shows the plot on grid
>> axis ([ -10 10 -1 1]);       % delimits the axes
>>  title (‘sin function’);     %sets title to the graph
>> xlabel (‘x’);        % sets x-label to ‘x’
>> ylabel (‘sin (x)’);       % sets y-label to ‘sin (x)’
>> legend (‘sin’);         % sets legend to ‘sin’
>> legend (‘location’, ‘north’);         % locates the legend at center and top
>>

```

We can make this plot more beautiful by adding some color or by increasing the line-width. For more clarity, type the command help plot.

We can use fplot () for making graph of functions. Using fplot () is a faster way of plotting which takes the function and the limits (in a vector form) as arguments.

```>> fplot (@sin, [-10 10]);        % creates the graph of sin function from -10 to 10
>> grid on;           % shows the plot on grid
>> title (‘sin function’);     %sets title to the graph
>> xlabel (‘x’);        % sets x-label to ‘x’
>> ylabel (‘sin (x)’);       % sets y-label to ‘sin (x)’
>> legend (‘sin’);         % sets legend to ‘sin’
>> legend (‘location’, ‘north’);         % locates the legend at center and top
>>

```

We can load a figure to a variable, say h, just by assigning it to h. The h is called graphics handle. It is a pointer to denote a figure by assigning each figure a unique number. For instance,

```>> gh = figure ();      % creates a graphics handle ‘gh’
>> fplot (@sin, [-3 3])
>> gh        % prints the value inside the graphics handle ‘gh’
ans   =   1
>> get (h, ‘type’)        % get () function prints property, here ‘type’, of the handle given to it
ans   =   figure
>>

```

Let’s see some manipulative commands.

Time to apply!

```>> fplot(@sin, [-5 5]);
>> xlabel (‘x’); ylabel (‘f (x)’);
>> hold on;        % holds the plot for addition of the following plot
>> fplot (@cos, [-5 5]);
>> legend (‘sin’, ‘cos’);      % sets legend to the plots in order
>> title (‘sine-cosine waves on the same plot’);
>>

```

Look at the subplot () function.

```>> subplot (2, 1, 1)       % creates a figure having two subplots in 2 x 1 matrix form, the third argument is the index of the subplot
>> fplot (@sin, [-5 5]) ;        % plots the graph on the first subplot
>> xlabel (‘x’); ylabel (‘sin (x)’)      % sets x-y labels
>> legend (‘sin’, ‘location’, ‘northwest’);      % sets legend to ‘sin’ and locates it at top and left of the subplot
>> title (‘sine wave’);        % sets title to ‘sine wave’
>> subplot (2, 1, 2)         % creates the second subplots
>> fplot (@cos, [-5 5]);       % plots the graph on the second subplot
>> xplot (‘x’); ylabel (‘cos (x)’)      % sets x-y label
>> legend (‘cos’, ‘location’, ‘northwest’)        % sets legend to ‘cos’ and locates it at top and left of the subplot
>>

```

Now, we have made many figures. It’s time to save it. Octave provides some formats to save the graphics.

Some useful formats are jpg, jpeg, png for images and others are pdf, however the default ps format. Its syntax is saveas (graphics_handle, ‘file_name’, ‘options’). We can set format inside options. For instance,

```>> gh = figure ();       % creates a graphics handle
>> fplot (@sin, [-3 3]);        % plot sine function
>> xlabel (‘x’); ylabel (‘sin (x)’);        % sets x-y labels
>> title (‘Sine Wave’);          % sets title
>> saveas (gh, ‘sine wave’, ‘jpg’);        % save the figure in the current directory with name ‘sine wave’ in ‘jpg’ format
>>

```

You should see the following jpg image in the directory.

Try other formats that suit you.

## Files I/O

Now, we will see some useful system commands on Octave. These commands are inherited from UNIX.

Let’s see some of them.

Practice it. This increases your interaction with the system.

Octave CLI (command line interface) saves our file in our desired path using save … command. For instance,

```>> save myfile              % saves myfile to the current directory
>> save desiredPath\myOctaveFiles\myfile1	  % saves myfile1 to the myOctaveFiles

```

Files are loaded also in Octave CLI using load …. command. For instance,

```>> load myfile              % loads myfile