lettura simple

Variables in Octave

In this lesson I'll explain how to use variables in Octave.

What is a variable? A variable is a memory space in which you can record a numeric or alphanumeric value. So you can recall the value in the variable without having to write it again. Each variable has a name that distinguishes it from the other variables of the program or session.

To assign a value to a variable you need to use the equality operator

nameVariable = value

On the left side write the name of the variable while on the right side the value to be assigned to the variable.

I'll give you a practical example

Create the variable year and assign it the value 2020

>> year = 2020

Octave assigns a memory address dedicated to the year variable and writes the numeric value 2020 in the space.

Now, type this operation

>> year + 1

Octave loads the value of the year variable from memory (2020) and adds 1. Then it saves the new value (2021) in the memory address.

The output result is

ans = 2021

Does a variable store only numeric values?

No, you can also use variables to store alphanumeric values.

In this case you have to put the alphanumeric value between two quotes.

>> name = 'Nigiara'

In general, a variable can contain integer values, real numbers with single or double precision comma, characters, logical Boolean values (1 or 0), complex numbers, etc.

How to choose a variable name

When choosing the variable name in Octave you have to respect the following rules

  • It must begin with a letter
    The variable name have to begin with a letter. The rest of the name can also consist of numbers.
  • You can use lowercase or uppercase letters
    Octave distinguishes lowercase letters from uppercase letters (case sensitive). So year and YEAR are considered two different variables.
  • You don't have to use symbols or special characters
    You cannot enter special characters (such as &, $, #, etc.) in the name of a variable, except for the underscore _ (underscore).
  • You don't have to use keywords
    The name of a variable must not coincide with keywords already used by Octave.

How to check if a word can be used as a variable name? Octave provides you with a special function to understand if a word is a keyword. It is the iskeyword ('name') function. In brackets you must indicate the name you would like to give to the variable between two quotes.
function iskeyword()
If the function returns 0 you can use the name to define the variable. If it returns 1, instead, it is a name already used by Octave. In this case you cannot use it.

A few practical tips

Use a variable name that remembers its content.

So you can quickly understand what the variable contains

>> year=2020

You can also use names that consist of multiple words

>> codeproduct = 'abcdefg'

In this case, it is better to use an unederscore to separate the words

Thus, you can more easily read the variable name

>> code_product = 'abcdefg'

Alternatively, you can use a capital letter for the first letter of the second word.

Also in this way the name becomes more readable.

>> codeProduct = 'abcdefg'

These are just tips. You can choose the variable name as you like.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin