TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Octave

Declare Variables in Octave

Overview

In this tutorial, we will learn how to declare variables in Octave with examples. We declare and use the variable name to assign the value and later use it in the script/function.

Declare Variables

We can use the variable name and the assignment operator to declare and instantiate the variable in Octave. We need to declare and define the variable before using it.

For example, if we try to use the variable x without defining it we get the following error:

error: ‘x’ undefined near line <line_number>, column <col_number>

To avoid such errors we can define and assign values to the variable before using the variable in the code.

Undefined variable Octave Error

Examples

Let’s see some examples to declare and assign the value to the variable.

Scalars

A scalar is a simple number or numeric variable. The size of the scalar variable will be 1×1.

a = 25;

g = 9.81;

Note that Octave is case-sensitive. Variables with lowercase letters are different than the variables with the uppercase letters.

For example, the variables x and X are not the same.

Octave is case sensitive

In the example, the variable x is a number with 25 assigned to it. However, the variable X holds the string ‘Hello’.

Example

Assign a value of 10 to a variable called rate.

>> rate = 10;
>> rate

rate = 10

When we end the statement with a semicolon the command window, Octave will suppress the output display.

If the semicolon is not used then the output will be displayed by the Octave. The format would be:

<variable_name> = <value>

We can type the variable name at the command prompt to know the value of the variable.

 

Octave Assign value to variable

Vectors

Vectors can be either a row matrix(only one row) or a column vector (having only one column). The size of the row vector would be 1xn ( n columns). The size of the column vector would be nx1 ( n rows).

We can declare the values inside square brackets and elements separated by commas.

For example, to declare and instantiate a row vector:

x = [ 2 ,4, 6];

For example, to declare and instantiate a column vector:

y = [ 2 ;4 ;6];

Matrices

A matrix has multiple rows and columns. The size of the matrix would be mxn ( m rows and n columns and m,n > 1)

We can declare the values inside square brackets and rows separated by a semi-colon. Each element in the row can be separated by space or comma(,)

Built-in Constants

In Octave, we can use some built-in constants. There is no need to declare and initialize these variables.

pi

The pi constant is the ratio of the circumference of a circle to its diameter.

To know the variables defined in the current session:

https://www.testingdocs.com/octave-who-command/

—

Octave Tutorials

Octave Tutorial on this website can be found at:
https://www.testingdocs.com/octave-tutorial/

More information on Octave can be found on the official website:
https://www.gnu.org/software/octave/index

Related Posts

Octave Package List

Octave /

Install Octave Packages on Linux

Octave /

Octave User defined Function Example

octave command line linux

Octave /

Octave Command line on Ubuntu

Hello World in Octave

Octave /

Hello World in Octave

Octave /

Octave plot example of function y=x^2

‹ Octave Increment & Decrement operators› Octave If Statement

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com