Variables

What are Variables?

A variable is virtual space to store numbers, Booleans, and text in Octave, which you can give a name to.

Syntax

To create a variable, you can type a variable name, followed by “=” and expression given as

variable_name = expression

There are a few rules that you need to follow to give a variable name.

  • variable_name should be either primary English alphabetic characters (i.e., “A-Z”, “a-z”), or combination of alphabetic characters, numbers, and/or underscore character (i.e., “_”).
  • variable_name must NOT include any spaces. It is also a case sensitive, e.g., abc ≠ Abc.
  • variable_name should be always in the left side of “=” to create variables (NOT right side of “=”).
  • expression can be numbers, Booleans, or text.
  • expression for text should be enclosed by single quotation marks, i.e., ‘expression’.

How to Create Variables with Numbers in Octave?

For example, you can create a variable “a” to store a number 10, by typing

>> a=10
a = 
    10

You can also use arithmetic expression to create the variables, i.e., create a variable “b” for the (1+3)/2. Octave assigns the final numeric value, 2 on the variable “b”.

>> b=(1+3)/2
b =
    2

Note Octave displays the value stored in the variable as soon as it is created. However, if you want to suppress the automatic, immediate display, you can use “;” (semicolon) at the end of the command.

>> a=10;
>> (nothing display, but internally 10 is assigned on the variable "a")

How to Create Variables with Text in Octave?

To create a variable “a_text” for text, for example, “I am a boy”, type

>> a_text='I am a boy'
a_text = 
I am a boy

Note that within the single quotation marks, you can have space(s), since it is a part of the text.

What if No Variable Name is Given?

If no variable name is given, e.g., direct numeric number or arithmetic expression in the command window, the variable “ans” automatically creates to store the most recent number, Boolean, or text value.

>> 1
ans = 
      1

The variable “ans” holds only the latest “answer”, i.e., the previous “answer” is deleted.

>> 1+2
ans = 
       3

The previous “answer”, ans = 1, is replaced with ans = 3, i.e., only the last “answer” is stored.

List Existing Variables

To display created variables, type “who”

>> who
   a   b

It displays the created variables “a” and “b”, but the variable names could be different as you might create the variables with different variable names. Note that “who” displays only the list of variable names.

To display detailed information of created variables, type “whos”

>> whos
Name   Size   Bytes     Class

 a     1x1      8      double
 b     1x1      8      double

It displays the created variables “a” and “b” including the size, memory used, and variable type of each variable, but the variable names, size, bytes, and class could be different as you might create the variables with different variable names, size, bytes, and class.

Clear Variables

To clear the created variables, type “clear” (or “clear all”) for the entire variables at once, or “clear” followed by the variable name(s) that you want to clear.

>> clear
>> clear all
>> clear a b

Arithmetic Operations

List of basic arithmetic operations in Octave is given below

OperatorsSymbols
Addition+
Subtraction
Multiplication*
Exponentiation^
Division/ or \

Addition:

>> 1+3
ans =
      4

Subtraction:

>> 1-3
ans =
      -2

Update Variable using Same Variable: Octave allows to use the same variables in left and right sides of “=”. This expression updates the variable on the left side of “=” based on the expression on the right side of “=”. Note: variables on the right side of “=” holds the values in the previous commands.

>> a=1;
>> a=a+1
a =  
    2

We have “a = 2” as a final outcome, since the variable “a” updates with “a+1”, since the previous value of “a” (on the right hand side of “=”) is 1.

Multiplication:

>> 1*3
ans =
      3

Exponentiation:

>> 2^3
ans =
      8

Division:

Octave has two division operators

  • “a/b” : the right division, i.e., a÷b
  • “a\b” : the left division, i.e., b÷a
>> rd = 2/1
rd =
     2
>> ld = 2\1
ld =
     0.5000

In this article

Scroll to Top