0% found this document useful (0 votes)
74 views

ERT355 - Lab Week 3 - Sem2 - 2018-2019

This document provides instructions for acquiring data, creating MATLAB scripts and functions. It discusses: - Importing data files into MATLAB using the Import Data tool, importdata and load commands. - Creating and running MATLAB scripts to automate computations and plot graphs. - Converting scripts into functions by adding input and output arguments to make programs more flexible and reusable. - Using functions like feval to evaluate functions specified as strings, and fplot to plot the values of functions.

Uploaded by

Shafira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
74 views

ERT355 - Lab Week 3 - Sem2 - 2018-2019

This document provides instructions for acquiring data, creating MATLAB scripts and functions. It discusses: - Importing data files into MATLAB using the Import Data tool, importdata and load commands. - Creating and running MATLAB scripts to automate computations and plot graphs. - Converting scripts into functions by adding input and output arguments to make programs more flexible and reusable. - Using functions like feval to evaluate functions specified as strings, and fplot to plot the values of functions.

Uploaded by

Shafira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 11

UNIVERSITY OF MALAYSIA PERLIS

SCHOOL OF BIOPROCESS ENGINEERING


(BIOSYSTEMS ENGINEERING)

SEMESTER 2 2018/2019

ERT 355 / 2

BIOSYSTEMS MODELLING AND DESIGN

LABORATORY MODULE - MATLAB

WEEK 3
ACQUIRING DATA

MATLAB SCRIPT

MATLAB FUNCTION
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

Contents

1 Objective...........................................................................................3

2 Acquiring data..................................................................................3

2.1 Import files into MATLAB.............................................................................3

2.1.1 Using Import Data tool......................................................................3


2.1.2 importdata command function.......................................................5
2.1.3 load command function....................................................................6
3 MATLAB scripts...............................................................................7

3.1 Creating a MATLAB script.............................................................................7

3.2 Make sure MATLAB knows the path to your script......................................8

4 MATLAB function.............................................................................8

4.1 Function with one output..............................................................................8

4.2 Function with multiple outputs.....................................................................9

4.3 feval...........................................................................................................10

4.4 fplot...........................................................................................................10

Page | 2
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

1 Objective

The objectives of this laboratory module are:

1. To be able to acquire external data file to be loaded into MATLAB.

2. To be able to create and use MATLAB scripts.

3. To be able to create and utilise MATLAB function.

2 Acquiring data

2.1 Import files into MATLAB

2.1.1 Using Import Data tool

You can import files (i.e., excel files, .txt, .dat, .csv and many more) into MATLAB
interactively by going to tab HOME and then click Import Data as shown in Figure 1.

Figure 1: Import Data

Page | 3
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

Then select the file that you want to be imported into the MATLAB and click Open as
shown in Figure 2.

Figure 2: Select and open the required files.

Figure 3 will be pop up and you can click Import Selection. Once clicked the Import
Selection button, you can close the pop up window by click X in top right corner.

Figure 3: Import Selection

Page | 4
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

If successful you can see the imported variables from your spreadsheet into MATLAB
in Workspace section.

Figure 4: Imported variables in MATLAB Workspace

2.1.2 importdata command function

You can also load the excel files or any types of files (i.e., .dat, .txt and .csv) by typing
importdata command in Command Window.

>> dataquiz1=importdata('ERT160Quiz1.csv')

dataquiz1 =

data: [62x2 double]

textdata: {'Matric Number' 'Quiz 1'}

colheaders: {'Matric Number' 'Quiz 1'}

The loaded data in Workspace is shown in Figure 5.

Page | 5
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

Figure 5: Loaded variables in Workspace

The numeric data is stored inside the variable dataquiz1.data. If you want to call
the numeric data you can type dataquiz1.data in Command Window.

Task 1:

1. Import all the signal_one, signal_two, signal_three and signal_four data into
MATLAB.

2. From the loaded file, plot the individual graph of x vs y for signal_one,
signal_two, signal_three and signal_four on Figure 1, Figure 2, Figure 3 and
Figure 4 respectively.

3. Create allsignaldata variable that contains x data in the first column, and all
the signal signal_one, signal_two, signal_three and signal_four y data in the
second, third, fourth and fifth column respectively.

2.1.3 load command function

You can also use load function to load the files that only contained numeric data.

Page | 6
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

Task 2:

temphumidata.txt file contains three columns of numbers. First column is the time of
the day, the second and third column is the temperature and humidity value for
Greenhouse 19 at Sungai Chuchuh.

1. Import the temphumidata.txt file into MATLAB using either Import Data tool,
importdata or load function.

2. From the loaded data, plot the average daily temperature and humidity for
Greenhouse 19 at Sungai Chuchuh. Label your graph correctly.

3 MATLAB scripts

So far, you have been typing commands into the Command Window. This is not a
good way to realise larger programs. Scripts are the simplest kind of program file
because they have no input or output arguments. They are useful for automating
series of MATLAB commands, such as computations that you have to perform
repeatedly from the command line or series of commands you have to reference.

3.1 Creating a MATLAB script

You can open a new script in the following ways:

1. Highlight commands from the Command History, right-click, and


select Create Script.

2. Click the New Script button on the Home tab.

3. Use the edit function.

This code opens the file file_name:

edit file_name

For example you can create a script called plotgraph.m

x=1:1:10;

y=x.^2;

plot(x,y,'r');

title('Plot of x against y');

Page | 7
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

xlabel('x');

ylabel('y=x^2');

You can run the code in plotgraph.m using either of these methods:

1. Type the script name plotgraph on the command line and press Enter

2. Click the Run button on the Editor tab

3.2 Make sure MATLAB knows the path to your script

Please be aware that you have to make sure MATLAB knows the path to your script.
MATLAB cannot execute a script unless it knows where to find its m-file. This
requires that the script be in the internal MATLAB path.

4 MATLAB function

4.1 Function with one output

For example, create a script in a file named forcecal.m that computes the force:

m = 5; %mass

a = 3; %acceleration

f = m.* a

After you save the file, you can call the script from the command line:

forcecal

f =

15

To calculate force using the same script, you could update the values of m and a in the
script and rerun it.

However, instead of manually updating the script each time, you can make your
program more flexible by converting it to a function. Replace the statements that
assign values to m and a with a function declaration statement. The declaration
includes the function keyword, the names of input and output arguments, and the
name of the function.

Page | 8
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

function f=forcecal(m,a)

f=m.*a;

After you save the file, you can call the function with different mass and acceleration
values from the command line without modifying the script:
a1 = forcecal(1,5)

a2 = forcecal(2,10)

a3 = forcecal(3,6)

a1 =

a2 =

20

a3 =

18

4.2 Function with multiple outputs

Create a function stat that contains following code:

function [m,s] = stat(x)

n = length(x);

m = sum(x)/n; %average

s = sqrt(sum((x-m).^2/n)); %standard deviation

Call the function from the command line.

values = [12.7, 45.4, 98.9, 26.6, 53.1];

[ave,stdev] = stat(values)

ave =

47.3400

stdev =

29.4124

Page | 9
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

4.3 feval

The function can be assigned to the variable h which is a string containing the name
of a function file.

>> h= 'forcecal'

feval evaluates the function specified by a function handle or function name.


feval is usually used inside functions which take function handles or function
strings as arguments.

>> feval(h,4,7)

Matlab program should return value of 28 which is 4 × 7.

4.4 fplot

Now try to use fplot to plot the value of the function.

Create a new function powercal.m that contains

function j=powercal(k);

j = k.^2

Type the following code in the Command Window.

m= 'powercal'

fplot (m,[0 100])

For further information on feval and fplot, at the main command window, type

>> help feval

>> help fplot

Page | 10
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)

Task 3:

Create a function name traparea.m that can calculate trapezoid area. The area of
trapezoid is given by the formula:

Area=h*(b1+b2)*0.5

Where h is the height and b1 and b2 is the length of the base. Calculate the trapezoid
area by using the built function with these parameters: h=7, b1=8 and b2=13. The
calculated trapezoid area is 73.5.

Task 4:

Create a function name plot2dgraph that can plot simple 2d graph based on the
assigned variables x and y. Label the graph appropriately. Try generate the graph of
x=[1:10] and y=4x2+3 data using the plot2dgraph function.

Task 5:

June2013tempdata.txt file contains four columns of data. First column is the


number of days in month June, the second column is the maximum daily
temperature, third column is average daily temperature and the fourth column is
minimum daily temperature for Malaysia in Jun 2013.

1. Create a script that load June2013tempdata.txt into MATLAB and plot all the
data using the 2d graph. Label the graph appropriately.

Page | 11

You might also like