[go: up one dir, main page]

0% found this document useful (0 votes)
19 views18 pages

OENG1278 Lectorial2

Uploaded by

shinysttuff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views18 pages

OENG1278 Lectorial2

Uploaded by

shinysttuff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

OENG1206 – Digital Fundamentals

Introduction to the MATLAB Environment

Lecturer: Dr Shruti Nirantar


Lectorial Overview

• In this lectorial we’ll be looking at the MATLAB environment


and some of the useful features it has.
• We’ll then continue to look at how we can use MATLAB to
generate data.
• Lastly, we’ll consider how data and files can be imported
into MATLAB such as Excel spreadsheets, text files,
images and audio.
Basic Numeric Variables in MATLAB

• To create simple, numeric variables in MATLAB you can assign


a number to a variable name followed by a semicolon (;).
• By default all numbers in MATLAB will be stored as a double-
precision floating point number, e.g.:
a = 1.5;
mass = 20;
vel_light = 2.99792e8; %Scientific notation!
• As you can see, a variable name can be a single letter or word.
It helps to have meaningful names for variables.
• Variables can also be created using mathematical operators:
mass = 20; % Mass of object (in kg)
accel = 1.5; % Acceleration of object (ms-2)
force = mass*accel; % Mathematically find force from
Newton’s law (F = ma)
Basic Character Variables in MATLAB

• Less commonly you may need to have letters or words as


variables (perhaps as labels for a graph).
• To create character arrays in MATLAB we can do the
following:
label_N = ‘North’;
label_S = ‘South’;
label_E = ‘East’;
label_W = ‘West’;
• The pink colour shows that these are characters.
• To convert a number to a character array we can use:
MyNum = 225; %Number stored as a double
MyNumStr = num2str(MyNum); %use num2str (number to
string) to convert double to characters.
Activity: Creating Variables in MATLAB

• Create a MATLAB script to calculate the acceleration, a in


the physical system shown below (all values must be
stored in appropriately named variables).

• For a physical system consisting of force (F), mass (m),


and acceleration (a) and assuming no friction, the
following equation applies:
F = ma
Activity: Creating Variables in MATLAB

• Use MATLAB to find the area of the following triangle (all


values must be stored in appropriately named variables).

• The area of a triangle can be given as:


A = ½.b.h
Activity: Creating Variables in MATLAB

• Create a MATLAB script to calculate the value of  in degrees


for the right-angled triangle shown below (all values must be
stored in appropriately named variables).

• MATLAB has trigonometric functions: sin(), cos(), tan(),


asin(), acos()and atan() which use radians. To convert
to degrees use ( is pi in MATLAB):
180 × 𝜃rad
𝜃deg =
𝜋
The input() Function

• The input() function is used to obtain an input from a


user on the Command Line.
• Typically the syntax is as follows:
x = input(‘Please enter a number: ’);
• This allows a user to enter a number into the Command
Line, this number will then be stored in the Workspace as the
variable x.
• Another usage of this function is to obtain text (string) input:
str = input(‘Please enter a word: ’, ‘s’);
• This will take in the text entered and store it in the
Workspace as the variable str.
The disp() Function

• The disp() function is used to display a variable or text


string on the Command Line. The syntax for this could be:
disp(x)
• This displays the contents of variable x on the command line.
• To output a text string to the command line you could use:
disp(‘Hello World!’)
• Lastly, you can concatenate strings and numeric variables
together and output the result:
str = [‘The answer is: ’ num2str(x)];
disp(str)
• This will output the text ‘The answer is: ’ followed by the
contents of the variable x to the command line.
The fprintf() Function

• The fprintf() function is used to output formatted text


to the Command Line.
• It’s similar to disp() but allows more flexibility with
formatting:
fprintf(‘%1.3f\n’, x)
• This outputs the variable x as a floating point number with
3 decimal place precision then a new line (\n is newline).
• The %f says the number is floating point, while the
numbers (1.3) dictate precision.
• Some other useful formats are: %s for a string array, %d or
%i for an integer and %e for exponential format
(e.g. 1.628910e-5).
The fprintf() Function

• Pure text can also be output similarly to disp() as follows:


fprintf(‘Hello World!\n’)
• This will display the string ‘Hello World!’ then start a new
line.
• Lastly, concatenating strings and variables together is a
little easier:
fprintf(‘The answer is: %1.3f\n’, x)
• Could output:
>> The answer is 1.325
fprintf(‘Converting %s to %s will give: $%1.2f\n’,
val1, val2, x)
• Could output:
>> Converting AUD to USD will give $56.50
Activity: Using the input() and disp() Functions

• Modify your MATLAB script that calculates the value of 


for the right-angled triangle so it now gets the values of O
and A from the user instead.

• Again, to convert to degrees use ( is pi in MATLAB):


180 × 𝜃rad
𝜃deg =
𝜋
Importing Data into MATLAB

• MATLAB can be used to import (and export) data into


your programs.
• Often you’ll have data in some form (image, waveform,
spreadsheet, text file, etc) that must be imported,
processed and exported to solve a problem.
• MATLAB has built-in functions to import and export data.
• These were covered in the lecture recording for this
week:
• readmatrix() (and export function writematrix())
• readcell() (and export function writecell())
• audioread() (and export function audiowrite())
• imread() (and export function imwrite())
Activity: Importing CSV Data

• Download the CSV file ‘scope1.csv’ from Canvas, the first


column is time and the second is voltage.
• Import the data into the MATLAB Workspace using the
readmatrix() function.
• Plot the voltage against time using a line graph.
• To plot one column of a matrix called x, against another
column you can use:
figure(1)
plot(x(:,1), x(:,2)) % Plots the second column of
x w.r.t. the first column of x
• We’ll look more at this next week when we cover arrays,
plotting and indexing.
Activity: Importing Excel Spreadsheets

• Download the spreadsheet ‘buildings.xlsx’ from Canvas.


• Import the spreadsheet into the MATLAB Workspace
using the readcell() function to begin with.
• Open the imported data through the MATLAB Workspace
and look at what’s stored in the variable.
• Compare this with what Microsoft Excel shows.
• Now, try importing this same spreadsheet using the
readmatrix() function.
• Take note of what happens to the text fields when you do
this.
Activity: Importing Audio Files

• Download the audio file ‘gameover.wav’ from Canvas.


• Import the audio and sampling rate into the MATLAB
Workspace using the audioread() function.
• Play the audio back using soundsc(), remember to use
the sampling rate imported with your audio to do this.
• Change the sampling rate to 30,000 then 60,000 and listen
to what happens when you play the sound back at the
wrong rate.
Activity: Importing Images

• Download the image file ‘barbara.png’ from Canvas.


• Import the image into the MATLAB Workspace using the
imread() function.
• View the image on a new figure window by using the
imshow() function. Also, look at the variable in the
Workspace to see how a greyscale image is stored
• Download and import the coloured image ‘lena.tiff’ and
repeat the above task. Take note how coloured images are
stored in the Workspace.
Finally

• This lectorial has introduced the MATLAB environment and


how to create simple script files.
• We started by looking at how we can create variables
including numeric and characters.
• Lastly, we looked at functions that can be used to import
and export common data types into MATLAB.

You might also like