ERT355 - Lab Week 3 - Sem2 - 2018-2019
ERT355 - Lab Week 3 - Sem2 - 2018-2019
SEMESTER 2 2018/2019
ERT 355 / 2
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
4 MATLAB function.............................................................................8
4.3 feval...........................................................................................................10
4.4 fplot...........................................................................................................10
Page | 2
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)
1 Objective
2 Acquiring data
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.
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 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.
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.
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 =
Page | 5
ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS LAB MODULE – MATLAB (WEEK 3)
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.
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.
edit file_name
x=1:1:10;
y=x.^2;
plot(x,y,'r');
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
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
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
n = length(x);
m = sum(x)/n; %average
[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(h,4,7)
4.4 fplot
function j=powercal(k);
j = k.^2
m= 'powercal'
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:
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