Content from What is MATLAB?


Last updated on 2024-10-29 | Edit this page

Overview

Questions

  • What does MATLAB do?
  • How can MATLAB help my Research?
  • Is MATLAB the best tool for my Research project?

Objectives

  • Explain what tasks MATLAB is well suited for
  • Explain how MATLAB compares to other common tools (Python, R)
  • Understand the trade-offs of choosing MATLAB as a tool

Introduction


What is MATLAB?

MATLAB is a high-level propriety programming language and interactive environment primarily designed for numerical computing, data analysis, and scientific visualization. It provides a comprehensive suite of tools for tasks such as matrix manipulation, algorithm development, data plotting, and interfacing with other programming languages. Its user-friendly interface and extensive library of functions make it a popular choice among researchers, engineers, and scientists.

Propriety

MATLAB is a propriety language, this means it is owned by a specific company, MathWorks. Therefore it’s source code is not publically available (i.e., it is the opposite of open-source) and the software is licensed for use under specific terms and conditions. There are many pros and cons of working with propriety software.

Discuss pros & cons

Discuss or think about what might be some of the advantages and disadvantages of software being owned by a single company.

Feature Proprietary Open Source
Cost Individual MATLAB license is £800/year (£59 one time for students) Nearly always free
Documentation Single documentation site with everything you need to know Can be spread around many site, different communities contributing different documentation
Support Single company to ask support queries and report bugs to Reliant on a spread out community for support with no single best place to go
Adoption Typically used in large companies and academia due to cost Widely adopted across all parts of the community

Why MATLAB?


While many research tools can accomplish tasks similar to MATLAB, understanding MATLAB’s strengths and weaknesses in comparison is important. Two of the main alternative languages you may have heard of are Python and R (r-project). These are often compared because all 3 languages are often used for research.

Low-level code languages are closer to the machine’s language, requiring more precise instructions for the computer to understand. They offer greater control over hardware but are more complex to write and debug. High-level code languages are more abstract and human-readable, translating simpler instructions into the complex machine code. They are easier to learn and maintain but may sacrifice some performance efficiency compared to low-level languages.

MATLAB is considered to be on the high-level end of languages, making it relatively easy to read and work with.

Key Points

  • MATLAB is used in many fields and can be used to analysis, process, model and much more

Content from Matlab Layout


Last updated on 2024-10-29 | Edit this page

Overview

Questions

  • What do all the different parts of the MATLAB interface do?
  • What features are important to get started in MATLAB?

Objectives

  • Learn to navigate and adjust the MATLAB interface
  • Understand the difference between the command window and the editor

Introduction


MATLAB as a language is mainly interacted with through the MATLAB application. Before we press on with learning, writing and running MATLAB, this episode will quickly run through the various parts of MATLAB and help you understand where and how you should write MATLAB.

Below is an annotated diagram showing what of the MATLAB application looks like.

A screenshot of the MATLAB application with red numbers annotating the major areas of the application which are detailed in the main body
Annotated MATLAB Application

Layout

Don’t worry if your layout does not match the one in the diagram. The MATLAB application is very customizable and you can drag and drop the various windows around. There are a few preset layouts available in the view tab at the top

Each number on the diagram describes:

  1. Menu Ribbon - This bar functions much like the bars at the top of many Microsoft and Google applications you may be familiar with, it has tabs with different options in. Throughout this course we will be exploring some of these tabs in more detail.
  2. Current Folder - Here you can explore the files on your computer much like the explorer on your computer. The folder currently showing is also known as the ‘Working Directory’ and this is where MATLAB will first search for files.
  3. Editor - This is where you will edit MATLAB code files.
  4. Command Window - One off MATLAB commands can be executed here.
  5. Workspace - You will be able to see any variables that are currently in memory here

If any of this isn’t clear again don’t worry! This course will further explore and clarify all of these tools.

Editor VS Command Window


You can write and run MATLAB code in both the command window and the editor so understanding the purpose (and differences) of each is important.

Command Window Editor
Quick calculations Creating and editing persistent scripts
Testing Reading and using others code
Interactive exploration Creating functions
Debugging

Challenge 1: Using the command window

What is the output of this command?

13 * 8

Challenge 2: Using the editor

Press ‘New Script’ at the top left and type the following into your editor. Then press ‘Run’ in the EDITOR tab at the top.

b = 136 / 8
  1. What do you have to do in order for the code to run?
  2. What is the output?
  1. The editor modifies and runs a MATLAB file (m-file), so in order to run what is in the editor you will be required to save the script you have made to a new m-file.

  2. 17

As you saw in the challenge above, using the editor requires you to create and save a file with the extension .m. This means that when you next come to do your analysis, processing, etc., you can reopen this file and continue work from it again.

Saving your work

Unlike the editor, any work done in the command window is not saved! This means that if you close MATLAB or your computer this work will be lost. Hence why you should use it only for quick temporary tasks.

Key Points

  • The MATLAB interface is very customisable, adjust it to suit you
  • Use the command window for quick tasks like exploring data, testing, etc.
  • Use the editor for developing code you want to persist between sessions

Content from Creating Variables


Last updated on 2024-11-06 | Edit this page

Overview

Questions

  • How can I store and work on data?
  • What does MATLAB assume about data?
  • How can I handle data of 2, 3 or more dimensions?

Objectives

  • Lean about what a variable is and how MATLAB handles variables
  • Understand how to create variables of various dimensions
  • Briefly review data types and why they aren’t important in MATLAB

Introduction


When programming there are many occasions where you will want to be able to access some data. For example, you may want to load in a data set from online, load data from an sensor in the field or store the results of a calculation. How we store and refer to data in programming is through the use of variables.

Variables


A variable in MATLAB is made of 2 parts, a name and a value. The name is what we use to refer to a variable and the value is the number, word, or item that is stored in it.

The value of a variable in MATLAB can be many things! Some examples are

Variable Type Example Value
Integer 1, 2, 3
Double 1.23, 4.56
Character ‘A’, ‘b’, ‘c’
String “hello”
Matrix [1 2 3; 4 5 6]
Logical true, false

As mentioned in the previous episode MATLAB has a section called the workspace, this is where you can view what Variables are in-memory. The simplest way of creating a variable is with the ‘=’ symbol, for example:

my_variable = 5

will create a variable with the name my_variable and a value of 5.

MATLAB (like most programming languages), holds variables in-memory. This means they are stored on your computers RAM rather than your hard drive. This allows for fast access and processing, however RAM is wiped when your computer turns off or an application quits so your workspace will be deleted when you close MATLAB or shut off your computer.

Challenge 1: Creating Variables

  1. Create a variable called A with a value of 3

  2. Create a variable called B with a value of ‘hello’

  3. Create a variable called a with the value of ‘C’:

  4. Create a variable called C with the value of the variable in A

  5. Customise your workspace and add the extra columns Size, Bytes and Class, this can be done by right clicking the top of the workspace

  1. A = 3
  2. B = 'hello'
  3. a = 'C'
  4. C = A
  5. Your workspace should look like this! We suggest customising the workspace columns just as an example of what is possible. A screenshot of the MATLAB workspace with 3 variables in it from the challenge

You may notice in the previous solution there is a type called a ‘double’. MATLAB is whats known as a dynamically typed language, which is also called “duck typing”. This is based off the saying ‘if it walks like a duck and quacks like a duck it’s probably a duck’. In programming terms this means that when you make a variable MATLAB has a look at it and assumes what type it is based on how it looks. Other languages like C++ require you to explicitly tell the program what type every variable is.

For you as a user, this means that you don’t have to really know or pay attention to data types! However it is worth knowing they exist, as if you get more advanced you may want to manipulate them to optimise your algorithms or for some other advanced use cases.

Variable Names


There are several conventions for naming variables, such as camel case, where each word is joined without a space and each new word starts with a capital letter. For example camelCase or analysisResult

Snake case is another common standard, where each word is lower case and separated with underscores, for example snake_case or analysis_results.

Both standards will work well, if you’re working with existing code or on an existing project it is normally best to stick with the convention already in use!

Useful Variable Names

In this lesson we are using variable names without any meaning such as A or B. When programming code it is good practice to use meaningful unique names for your variables.

Dimensions


A lot of data analysis, processing and workflows wont be with single numbers but with large multidimensional datasets. Working with image/video, time series, tables or many other data sources can quickly make your incoming data large in size. Variables in MATLAB can and are very good at storing and working with multidimensional data.

Penny command

Use the penny command in the command window. this command will create an interactive 3D plot of a US penny mold.

  1. How many new variables are there in your work space?
  2. Look at the size column in your workspace, how many dimensions are there in each variable?

If you double click a variable name in the workspace you can explore it in a spreadsheet style interface called the variable editor.

There should be 2 new variables in your workspace, P and D. Both have a size of 128x128, this means they have 128 rows and 128 columns and are therefore 2-dimensional matrices.

Dimension Order

In MATLAB learning the order in which dimensions appear is very helpful.

The first dimension is always rows and the second columns. So if you saw a variable with size 5x10, you could picture it looking like a spreadsheet with 5 rows and 10 columns. (This is the opposite if what you would expect from X, Y notation!)

In MATLAB there are names given to different variable shapes:

Dimensions Name Description
0D Scalar A single value like those created in Challenge #1
1D Vector A single row or column
2D Matrix Rows and columns, like an Excel spreedsheet or Google sheet
3D+ Array Rows, columns and pages
An animated GIF showing a vector column, matrix and array being built. It aims to add intuation for the shape of each of these types
Animated figure showing a vector, matrix & array

Creating Multidimensional Variables

In general when creating multidimensional variables you

  • use square brackets []
  • separate columns with a space
  • separate rows with a semi-colon ;

Here are some examples of creating vector variables:

MATLAB

E = [1 2 3] # 1x3 row vector named E 
F = [4;5;6] # 3x1 column vector called F

Challenge

  1. Create a row vector called G with values 2, 4 & 6
  2. Create a column vector called H with values 1, 3 & 5
  3. Create a 2x2 matrix called I with values 10, 20, 30 & 40.
  1. G = [2 4 6]
  2. H = [1;3;5]
  3. I = [10 20; 30 40]

Cleaning the Workspace


Finally we will look at how we can remove variables we aren’t using! We do this because as you explore data and test your analysis over time you will get variables you aren’t building cluttering your workspace, this can lead to you either accidently overwriting data or using variables you weren’t intending to.

Clearing all variables

Simply putting clear into your command window will clear all variables

Clear single variable

clear A will clear just the variable called A

Clear command window ## TODO clearup history After working a while you will have a long history of commands in your command window, you can clear this up by running the clc (command line clear) command.

Callout

You can access commands you have previously typed in the command window by pressing the up arrow on your keyboard. If you type part of a command and press the up arrow, only commands that match the partially completed command will be shown.

Indexing


Colon Notation

MATLAB provides what is called the colon notation which allows us to specify a range of values.

MATLAB

a = 1:10

OUTPUT

a =
     1     2     3     4     5     6     7     8     9    10

a should be 1x10 size, meaning 1 row 10 columns

Steps

You can also specify a step, so the colon notation only makes every nth number

MATLAB

b = 1:2:20

OUTPUT

b =
     1     3     5     7     9    11    13    15    17    19

Steps can also be non-integer values

MATLAB

c = 1:0.1:2

OUTPUT

c =
    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000    1.8000    1.9000    2.0000

Steps and range end values

When you specify a step that is grater than 1, the last value won’t necessarily be the range end value! Instead it will be the closest value that doesn’t go over the end value and this is why the last value in b is 19 and not 20!

Functions


We can also use functions to create arrays (vectors or matricies). Functions are premade code blocks that serve a commonly wanted functionality.

First we will use one called linspace, which stands for linearly spaced. It creates a vector of linearly spaced numbers, you specify the start, end and how many numbers.

Callout

If you are ever unsure about a function MATLAB has inbuilt help and documentation!

The help command will give a brief text description about the function and how to use it

MATLAB

help linspace

The doc command will give a more detailed description including examples

MATLAB

doc linspace

MATLAB


d = linspace(1,10,5)

OUTPUT


d =
    1.0000    3.2500    5.5000    7.7500   10.0000
    

Some other useful functions are:

MATLAB

% Rand creates an n-dimensional array of random numberes between 0 and 1.
e = rand(5,1)

% Create 2D matrix of zeros.
f = zeros(2,2)

% Create a 2D matrix of NaN values - NaN means 'Not a Number'
% This is a special and useful term for when a value can't be represented by a number.
g = nan(5,5)

Callout

Here are some common scenarios where NaN may be used:

  • Mathematical operations that cant be computed (division by 0, root of negative numbers)
  • No data were recorded, for example an in-field sensor may have lost power

Challenge

  1. Create a vector with numbers 12 to 100 containing every 4th number (steps of 4), called h
  2. Create a matrix of random numbers with 4 rows and 5 columns, call it ii
  1. h = 12:4:100 or h = linspace(12,100,23)
  2. ii = rand(4,5)

i

i in MATLAB can be used as a variable, but it also has a special meaning as the square root of -1! MATLAB treats j the same way. To avoid confusion it’s often better to use ii and jj instead.

Key Points

  • Variables are the main way we access and use data in MATLAB
  • Variables can store many types of data including multidimensional data
  • Creating multidimensional arrays is done with square brackets

Content from Working with Variables


Last updated on 2024-11-06 | Edit this page

Overview

Questions

  • How can I fetch data out of a variable?
  • How can I change the values of parts of variables?
  • What are operations?

Objectives

  • Access MATLAB variables and change them
  • Use variables to execute mathematical operations and in functions

Introduction


So far we have learnt how to create variables of various sizes with different methods. This episode will now focus on how ways we can use variables.

Now is a good time to clear and clc your workspace and command window!

Extracting Variables


First lets start by making a dummy variable that we can use as our stand-in dataset.

MATLAB

data = 100*rand(6,4);

Subsets

Subsets of values or single values can be extracted from a variable with round brackets ()

For example to take the value on the third row and second column:

MATLAB

data(3,2)

We can use colon notation to extract multiple values as well:

MATLAB

% Extract columns 1, 2, and 3 on row 3
data(3,1:3)

A single colon with no numbers will select all

MATLAB

% Select all rows in the second column
data(:,2)

Some more examples bringing together the tools we’ve seen so far:

MATLAB

% Extract the first 4 rows and all columns and save it in the variable subset1
subset1 = data(1:4,:) 

% Select every other column in the first row, save in subset2
subset2 = data(1,1:2:end)

% Select the first, third and forth rows for all columns, save in subset3
subset3 = data([1 3 4], :)

end

When extracting the subset of a matrix, or slicing it, when specifying the range you can use the keyword end to represent the last value in a vector (or row or column of a matrix, etc.).

Altering Variables


As we have seen so far, the object on the left hand side of the equal sign ‘=’ is set to what is on the right hand side. So to change the value of a variable we simply put it on the left:

MATLAB

% Set the value on the 3rd row and 2nd column to equal 10
data(3,2) = 10

If you look at data in your workspace now you will see that value has been changed!

One use case of altering data may be when you find an erroneous value. For example, if you were looking at a table of reviews out of 5, you may wish to change a rating that was somehow set to above 5 to NAN, to show it was invalid.

MATLAB

data(3,2) = NaN

Transpose

One useful tool in manipulating matrices (plural of matrix) in MATLAB is the transpose. This will effectively pivot the matrix so each row becomes a column and each column becomes a row. This is done in MATLAB by adding an apostrophe ' after a variable:

MATLAB

% Transpose data and save it as data_t
data_t = data'

You should see that data_t has a flipped size compared to data

Concatenation

Concatenation is a common operation in data handling. Concatenating means to link or put together, it allows to to take two matricies or variables and add them into a single variable. This is useful for if example your dataset is saved across multiple files.

First let’s clear our workspace again, create a new data variable and some subsets of the data to work with.

MATLAB


clear
data = 100*rand(6,4);

subset1 = data(:,1);
subset2 = data(:,2);

Both our subsets are column vectors, if we wanted to concatenate them together into a larger column vector there are three ways

MATLAB

new_data = [subset1; subset2]

new_data = cat(1, subset1, subset2)

new_data = vertcat(subset1, subset2)

All [;], cat, and vertcat are all different ways to do the same vertical concatenation.

Callout

Don’t forget if you find a function you aren’t familiar with you can use help or doc to learn more!

If we wanted to concatenate the subsets into 1 variable as separate columns we could do

MATLAB


new_data2 = [subset1 subset2]

new_data2 = cat(2,subset1,subset2)

new_data2 = horzcat(subset1,subset2)

One advantage of using cat is that it can work for arrays of larger dimensions, whereas the square bracket shortcut, vertcat, and horzcat only works for the first two dimensions of the data.

Challenge 1

  1. Extract every other row from Data assign it to the varibale name subset_a

  2. Extract the first four rows from the 2nd column of Data and call it subset_b

  3. Transpose subset_b, call this variable subset_t

  4. Concatenate subset_a and subset_t along the first dimension

MATLAB

% Extract every other row from Data assign it to the varibale name subset_a
subset_a = data(1:2:6,:)

% Extract the first four rows from the 2nd column of Data and call it subset_b
subset_b = data(1:4,2)

% Transpose subset_b, call this varibale subset_t 
subset_t = subset_b'

% Concatenate subset_a and subset_t along the first dimension
subset_concatenated = cat(1, subset_a, subset_t)

subset_concatenated should be of size 4x4

Operations


We’re now going to look at some mathematical operations we can perform on our variables.

Before continuing this is a good point to clear your workspace again and make a new dummy data variable. This time we will round each data point to the nearest whole number with the function round

MATLAB

clear
data = 100 * rand(10,10)

% Round to nearest whole number and overwrite data variable
data = round(data)

You may be familiar with the syntax (syntax is a word typically used in programming to mean format) of the most basic operators from other computer programs.

MATLAB


data_add = data + 10;
data_subtract = data - 10;
data_divide = data / 10;
data_multiply = data * 10;

One common mistake made by users of MATLAB is with the multiply operator. When multiplying pay attention to make sure you are getting the result you expect!

Challenge 2

  1. Make a row vector called row with values 1, 2 & 3
  2. Make a column vector called column with values 4, 5 & 6
  3. Before trying to multiply them, guess the size of the result of row * column
  4. Multiply row and column and see if the result is as you expect

MATLAB


row = [1 2 3];
column = [4; 5; 6];
row * column

OUTPUT


ans =
    32

The resulting variable is a scalar! If you are familiar with matrix multiplication this may make sense to you, don’t worry if you are not however.

As seen in the challenge above, by default MATLAB will attempt to perform something called matrix multiplcation, you don’t need to know about matrix multiplication but it is worth knowing it is the default behaviour.

What you may expect to have been doing is something called dot multiplication.

MATLAB


row2 = [4 5 6];
row.*row2

OUTPUT

ans =

     4    10    18

As the example above shows, dot multiplication multiplies each element of both variables with each other 1 to 1. This is why it is also sometimes called element-wise multiplication.

Functions


Next we will look at some key functions that you may want to use in data analysis and processing

MATLAB


% Find the size of a matrix
matrix_size = size(data)

% Add together the rows in each column
column_totals = sum(data, 1)

% Take the mean of the rows in each row
row_means = mean(data, 2)

% Find the maximum value in each column
data_max = max(data)

% Find the minimum value in each row
data_min = min(data, [], 2)

% Find the maximum value of the entire matrix
data_max_all = max(data, [], "all")

Indian Rainfall Example


In this example we will combine the tools we have learnt so far to compare rainfall data between Sheffield and India

Each dataset comprises of monthly rainfall averages per year from 1901-2014.

Sheffield (https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/sheffielddata.txt) Peninsular India (https://www.tropmet.res.in/DataArchival-51-Page)

MATLAB

% Import rainfall data. 
sheffield_data = load('Sheffield_Rain.csv');
india_data = load('SouthIndia_Rainfall.csv');

Challenge

Investigate the data, open it up in the workspace and think about what is in each column

Each dataset contains 13 columns, the first column is the year and the other 12 are for each month of the year

Challenge

  1. Create a subset of sheffield_data and india_data that contains only the rain data, call these subsets sheffield_rain and india_rain

  2. The Indian rainfall series is in tenths of a millimeter. Convert to millimeters by dividing each point by 10

  3. The columns contain monthly rainfall, find the average monthly rainfall over the 114 years. Call these new variables india_monthly and sheffield_monthly

MATLAB

% Select all data except the first column
sheffield_rain = sheffield_data(:,2:end);
india_rain = india_data(:,2:end); 

% Convert india_rain to millimeters
india_rain = india_rain/10;

% Take the mean of each column to find the average monthly rainfall
sheffield_monthly = mean(sheffield_rain, 1);
india_mothly = mean(india_rain, 1);

If your sheffield_monthly and india_monthly variables are correctly made, you should be able to run the following code to generate a bar chart comparing the two average rainfalls.

MATLAB

bar([1:12],cat(1, india_mothly, sheffield_monthly),'grouped')
legend('South India', 'Sheffield')
ylabel('Rainfall (mm)')

Key Points

  • Use functions to create, edit and operate on variables
  • Operations are used to perform simple mathematical functions
  • help and docs are valuable tools for understanding functions