Académique Documents
Professionnel Documents
Culture Documents
15.2 Creating and Calling Functions: (1/6) Creating and Calling Functions
Creating and Calling Functions
In this case, because only one output is needed, you do not need
to enclose the output in square brackets.
Call the two functions from the script and store %Modify the function definition
the five largest elements in a vector
named top5 and the five smallest elements in a function t = getLargestN(v,N)
vector named bottom5. sortedV = sort(v,'descend');
t = sortedV(1:N);
end
function b = getSmallestN(v,N)
sortedV = sort(v,'descend');
b = sortedV(end-N+1:end);
end
15.2 Creating and Calling Functions: (5/6) Electricity Usage Analysis Function
The script shown contains a function that takes function predictedusage = polyPrediction(filename)
the name of the data file as an input, fits a
predictdate = datetime(2020,1,1);
polynomial of degree 3, and returns the predicted
electricity usage on Jan 1, 2020. degree = 3;
The function also plots the actual and fitted usage % Import data
values.
TASK data = readtable(filename);
dates = data{:,1};
1. Modify the function definition so that the
usage = data{:,2};
function takes three inputs – the filename, sector = data.Properties.VariableNames{2};
the date on which the prediction is made,
and the degree of the polynomial. % Fill in the missing values.
usage = fillmissing(usage,'spline');
2. Also modify the body of the function such
that predictdate and degree are not % Fit polynomial and predict usage for a future date.
hardcoded. elapsedYears = years(dates-dates(1));
3. Now, modify the function call to predict c = polyfit(elapsedYears,usage,degree);
the usage on July 01, 2019 by fitting a
endDuration = years(predictdate-dates(1));
polynomial of degree 2.
usageFit = polyval(c,elapsedYears);
predictedusage = polyval(c,endDuration);
Task 1
Modify the function definition
function predictedusage =
polyPrediction(filename,predictdate,degree)
% Import data
data = readtable(filename);
dates = data{:,1};
usage = data{:,2};
sector = data.Properties.VariableNames{2};
endDuration = years(predictdate-dates(1));
usageFit = polyval(c,elapsedYears);
predictedusage = polyval(c,endDuration);
filename_ind = 'elec_ind.csv';
predictdate2 = datetime(2020,01,01);
ind_predictedusage =
polyPrediction(filename_ind,predictdate2,4);
Task 1
Modify the function definition
function predictedusage =
polyPrediction(filename,predictdate,degree)
% Import data
data = readtable(filename);
dates = data{:,1};
usage = data{:,2};
sector = data.Properties.VariableNames{2};
endDuration = years(predictdate-dates(1));
usageFit = polyval(c,elapsedYears);
predictedusage = polyval(c,endDuration);
To run the function, you should call the function with the necessary input arguments from the Command
Window or from another function or a script.
Using functions creates fewer variables in the base workspace, which results in fewer variable name conflicts. This
lesson focuses on function workspaces.
Suppose you have three
scripts.
15.5 MATLAB Path and Calling Precedence: (2/7) Adding Folders to MATLAB Path
Adding Folders to MATLAB Path
The search path, or path is a subset of all the folders in the file system. MATLAB can access all files in the
folders on the search path.
To add folders to the search path:
On the Home tab, in the Environment section, click Set Path.
Add a single folder or a set of folders using the buttons highlighted below.
elecData = readtable('elec_res.csv');
date = elecData.Dates(1)
date =
01-Jan-1990
However, MATLAB has an in-built function also
named date that returns a character vector
containing today's date.
nextDate = date + 1
?
n MATLAB, there are rules for interpreting any elecData = readtable('elec_res.csv');
named item. These rules are referred to as the
date = elecData.Dates(1);
function precedence order. Most of the common date =
reference conflicts can be resolved using the 01-Jan-1990
following order:
nextDate = date + 1
nextDate =
Variables 02-Jan-1990
Functions defined in the current script
Files in the current folder
Files on MATLAB search path
(T/F) When you enter the Hint – Only directories on the MATLAB path will be searched.
command
myFunction
MATLAB will always search
your entire computer for a file
named myFunction.
True
False
15.5 MATLAB Path and Calling Precedence: (7/7) MATLAB Calling Precedence
TASK
Create a variable named sum with value equal to 5.
Sum = 5
You just created a variable named sum. However, TASK
MATLAB also has a build-in function named sum. Use the which command to determine whether the
function sum or the variable sum has the higher priority.
You can use the which function to determine which file (Do not save to a result.)
or variable with a given name has the highest priority in
the current context.
which sum
which name
It is possible to have multiple functions that are stored in TASK
different folders but have the same name. You can also Display the file location for all instances of items called sum.
use the which function to see all the files and/or (Do not save the result.)
variable on MATLAB's search path with a given name. which sum -all
Therefore, the first four pyramid numbers, for
example, are:
P(1) = 1
P(2) = 1 + 4 = 5
P(3) = 5 + 9 = 14
P(4) = 14 + 16 = 30
function P = pyramid(n)
P = sum((1:n).^2);
End
TASK Khufu = pyramid(210)
The Pyramid of Khufu, also known as the Great
Pyramid of Giza, is estimated to have had 210
stone layers when it was originally built.