Variables and Functions

Module Overview

while completely_lost

    type_code = gibberish

    if error
        try_again = true
    elseif indecipherable_error
        bang_on_keyboard = true
    else
        scream_loudly_into_the_void
    end


end

So, you’re being forced to learn computer programming. Don’t worry. It’s not that bad once you get the hang of things. Computer programming basically involves moving data (words, pictures, numbers, etc) in and out of computer memory. To do so requires the use of a computer language, which all have very specific rules on how they are written (syntax). Just as in any language, the order of words, spacing, and punctuation all matter. Critically. (See Lynne Truss’s delightful book “Eats, Shoots and Leaves” on punctuation in the English language if you don’t believe me). In fact, the biggest hurdle and most frustrating part of learning computer programming is getting the syntax exactly right.

The most basic tasks in computer programming involve the creation of variables and the shuttling of said variables into functions. We’ll talk about what those are in this module.

  1. What Is a Variable?
  2. What is a Function?

Learning Objectives

After completing this Module, you should be able to:

  • Define Data structure and array
  • Describe the difference between a Matrix, an Array, and a Vector
  • Explain variable Indexing and compare the difference between linear and standard indexing
  • Define Data Type and list the fundamental MATLAB classes
  • Define a function (or sub-routine).
  • Know how to find and call built-in MATLAB functions using the proper syntax.

Important Terminology

  • Variable: a symbolic name associated with a changeable value.
  • Data Structure: a particular way of storing and organizing data in a variable
  • Data Type: a variable classification that determines the types of values that are allowed to be stored in the variable
  • Array: a simple type of data structure consisting of a collection of elements (values or variables). Each element can be identified by an index
  • Vector: A one-dimensional array of numbers (a single row or column of numbers)
  • Matrix: A two-dimensional, rectangular array of numbers. The basic format of a MATLAB Variable. All MATLAB variables are arrays (think spreadsheet)
  • Syntax: the precise coding language of a computer program.

What Is a Variable?

TOP || What Is a Variable? | What is a Function?

find_x
find_x

As we learned back in algebra, a variable in math is a symbol that represents a quantity in a mathematical expression. In computer programming, variables are symbolic names that are associated with values. These values can be numbers, letters, words, or even a whole algorithm.

Technically, Variables are storage locations (in your computer’s memory) that contain data. Variables have names, such as ‘A’ or ‘apple’, and their contents can be changed (hence the term variable). Variables are fundamental components of programming.

Essentially, a computer program creates or assign values to variables and then manipulate those values using step-by-step procedures (known as algorithms).

What Is a Data Structure?

TOP || What Is a Variable? >> What Is a Data Structure? | What Is Indexing? | What Is a Data Type? |

A data structure is a particular way of storing and organizing data so that it can be used efficiently. Variables can have different data structures. One of the most fundamental data structures is the array:

  • Array: One of the simplest types of data structures, an array consists of a collection of elements (or values), which can each be identified by an index.
  • Scalar: an array with just one element (and one value)
  • Matrix: MATLAB puts the MAT in matrices. The most basic MATLAB data structure is the matrix: a two-dimensional, rectangularly shaped array capable of storing multiple elements of data in an easily accessible format. (See Creating Matrices). Think of what a spreadsheet looks like, and you have an idea of what a matrix is. All of the data that you enter into MATLAB is stored in the form of this multidimensional type of array.
Examples of arrays
Examples of arrays

What Is Indexing?

TOP || What Is a Variable? >> What Is a Data Structure? | What Is Indexing? | What Is a Data Type? |

Indexing refers to a shortcut system that can point to elements in an array.

In a matrix, each element can be referenced by its index (or coordinates). MATLAB actually uses two different indexing systems: standard or linear indexing.

Indexing Systems
Indexing Systems

On the left is the linear indexing system, which has a COLUMN MAJOR indexing. Notice that in this scheme each element is numbered first by row, then by column. So, to refer to the element in row 2, column 2, you simply refer to the index number 5. On the right is the standard indexing system, which has the following syntax: (row, column). In this scheme, the left number in the pair of numbers indicates the row and the right number indicates the column. To refer to the element in row 2, column 2, you would use the following syntax: (2,2). These two indexing schemes can be used interchangeably in MATLAB.

What Is a Data Type?

TOP || What Is a Variable? >> What Is a Data Structure? | What Is Indexing? | What Is a Data Type? |

A data type is a variable classification that determines its possible values. In MATLAB, a data type is also known as a Data Class. For example, a variable that has a numeric class can only store numbers while a variable that has a char class can only store characters and strings.

In this course, we will learn about the following MATLAB classes:

Class Name Intended Use
numeric numbers
char characters
string strings (eg words)
cell complex content
struct hierarchal data
logical boolean (true / false) data
table Complex Spreadsheet type data

The numeric class is actually comprised of a number of subclasses for storing Floating-Point Numbers (i.e not whole numbers) and Integers:

Floating-Point Number Classes

  • single, double

double is the default numeric class

Integer Classes

  • int8, uint8, int16, uint16, int32, uint32, int64, uint64

As we’ll see in later modules, a variable’s numeric class can affect the minimum and maximum numbers that can be stored in that variable.

What is a Function?

TOP || What Is a Variable? | What is a Function?

A function (or a sub-routine) is a packaged sequence of programming instructions designed to perform a specific task. Often, these programming instructions are hidden from the user, so you can think of a function a little like a black box.

Functions often take inputs (in the form of variables) and return output (other variables)

The MATLAB syntax used to call a function can be summarized as follows:

output = name_of_function(input_variable)

MATLAB has thousands of built-in functions to perform almost any basic thing you can think of. If not, you can search the internet for MATLAB functions other people have written. There’s a lot out there

In the MATLAB documentation (under the help menu), you can find the available MATLAB functions and the proper syntax for calling those functions .