Creating Functions
-Last updated on 2023-12-04 | +
Last updated on 2024-11-18 | Edit this page
@@ -376,24 +376,25 @@Objectives
Writing functions from scratch
It has come to our attention that the data about inflammation that we’ve been analysing contains some systematic errors. The measurements -were made using the incorrect scale, with inflammation recorded in -Arbitrary Inflammation Units (AIU) rather than the scientific standard -International Inflmmation Units (IIU). Luckily there is a handy formula -which can be used for converting measurements in AIU to IIU, but it +were made using the incorrect scale, with inflammation recorded in units +of Swellocity (swell) rather than the scientific standard units of +Inflammatons (inf). Luckily there is a handy formula which can be used +for converting measurements in Swellocity to Inflammatons, but it involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
There are twelve files worth of data to be converted from AIU to IIU: -is there a way we can do this quickly and conveniently? If we have to -re-enter the conversion formula multiple times, the chance of us getting -the constants wrong is high. Thankfully there is a convenient way to -teach MATLAB how to do new things, like converting units from AIU to -IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity +to Inflammatons: is there a way we can do this quickly and conveniently? +If we have to re-enter the conversion formula multiple times, the chance +of us getting the constants wrong is high. Thankfully there is a +convenient way to teach MATLAB how to do new things, like converting +units from Swellocity to Inflammatons. We can do this by writing a +function.
We have already used some predefined MATLAB functions which we can pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a @@ -433,19 +434,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
MATLAB<
OUTPUT
@@ -463,41 +465,43 @@ OUTPUT<
OUTPUT
@@ -463,41 +465,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a -script can be thought of as automating the command line, with full -access to all variables in the base workspace, whereas a function has -its own separate workspace.
+script automates the command line, with full access to all variables in +the base workspace, whereas a function has its own separate +workspace.To be able to access variables from your workspace inside a function, you have to pass them in as inputs. To be able to save variables to your -workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them +as outputs.As with any operation, if we want to save the result, we need to assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
And we can see val_in_IIU
saved in our workspace.
And we can see val_in_inf
saved in our workspace.
OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
Writing your own conversion function
We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
For reference the conversion formula to take inflammation measured in +Swellocity to inflammation measured in Inflammatons is:
+Writing your own conversion function
-MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
Functions that work on arrays
needs. The function will take the variablepatient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB script!
@@ -618,12 +631,12 @@MATLAB< MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command line:
-OUTPUT
@@ -650,18 +663,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used inside the function.
If we now run our function in the command line, we get:
-OUTPUT
@@ -681,17 +694,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -748,47 +761,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -823,42 +836,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -945,7 +958,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/aio.html b/aio.html
index 59145d4..1a681aa 100644
--- a/aio.html
+++ b/aio.html
@@ -1315,7 +1315,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1519,7 +1519,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1852,7 +1852,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2154,7 +2154,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2173,7 +2173,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2467,7 +2467,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3644,7 +3644,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -3685,24 +3685,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3742,19 +3743,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3763,7 +3765,7 @@ MATLAB<
OUTPUT
@@ -3772,41 +3774,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3814,13 +3818,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3829,20 +3842,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3889,39 +3902,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3929,12 +3942,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3961,18 +3974,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -3992,17 +4005,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4059,47 +4072,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4136,42 +4149,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor-notes.html b/instructor-notes.html
index 82e7893..ca35423 100644
--- a/instructor-notes.html
+++ b/instructor-notes.html
@@ -405,7 +405,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/instructor/02-arrays.html b/instructor/02-arrays.html
index e0e69c5..16981f9 100644
--- a/instructor/02-arrays.html
+++ b/instructor/02-arrays.html
@@ -630,7 +630,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -832,7 +832,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
diff --git a/instructor/03-loading_data.html b/instructor/03-loading_data.html
index 27d4ee9..3728447 100644
--- a/instructor/03-loading_data.html
+++ b/instructor/03-loading_data.html
@@ -578,7 +578,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -876,7 +876,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -895,7 +895,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
diff --git a/instructor/04-plotting.html b/instructor/04-plotting.html
index 1488ba5..717c759 100644
--- a/instructor/04-plotting.html
+++ b/instructor/04-plotting.html
@@ -563,7 +563,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
diff --git a/instructor/07-func.html b/instructor/07-func.html
index 5dacebc..f9ad592 100644
--- a/instructor/07-func.html
+++ b/instructor/07-func.html
@@ -335,7 +335,7 @@
Creating Functions
- Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -378,24 +378,25 @@ Objectives
Writing functions from scratch
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -435,19 +436,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -456,7 +458,7 @@ MATLAB<
OUTPUT
@@ -465,41 +467,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -507,13 +511,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -522,20 +535,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -580,39 +593,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -620,12 +633,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -652,18 +665,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -683,17 +696,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -750,47 +763,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -825,42 +838,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -947,7 +960,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/instructor/aio.html b/instructor/aio.html
index f2a7fc7..f373fcb 100644
--- a/instructor/aio.html
+++ b/instructor/aio.html
@@ -1319,7 +1319,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1857,7 +1857,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/md5sum.txt b/md5sum.txt
index 8f43bdb..7217342 100644
--- a/md5sum.txt
+++ b/md5sum.txt
@@ -9,7 +9,7 @@
"episodes/04-plotting.md" "686b418946e7b164a392c060ad096705" "site/built/04-plotting.md" "2024-03-22"
"episodes/05-scripts.md" "56258cbb1e04bde1d86d974fa3f20e7c" "site/built/05-scripts.md" "2023-12-08"
"episodes/06-cond.md" "c8e7b50395e7d26ede321c9a23284726" "site/built/06-cond.md" "2023-12-04"
-"episodes/07-func.md" "4af772aa1e02408c453cdaabef404e3c" "site/built/07-func.md" "2023-12-04"
+"episodes/07-func.md" "950fb0b2819db046b3b546555c1813f7" "site/built/07-func.md" "2024-11-18"
"episodes/08-loops.md" "e00de44cbbcc50ab075fcb51f337953a" "site/built/08-loops.md" "2023-12-04"
"instructors/instructor-notes.md" "95bc9dd61103b3d6345f9d96dce4538b" "site/built/instructor-notes.md" "2023-10-18"
"learners/reference.md" "938a4e51e92ebb2e3cf8feb3fc932cd0" "site/built/reference.md" "2023-12-04"
diff --git a/pkgdown.yml b/pkgdown.yml
index d485019..27ab95d 100644
--- a/pkgdown.yml
+++ b/pkgdown.yml
@@ -2,4 +2,4 @@ pandoc: 3.1.11
pkgdown: 2.1.1
pkgdown_sha: ~
articles: {}
-last_built: 2024-11-18T14:28Z
+last_built: 2024-11-18T15:00Z
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs, so in the command line, we run it as:
-MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -748,47 +761,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -823,42 +836,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -945,7 +958,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/aio.html b/aio.html
index 59145d4..1a681aa 100644
--- a/aio.html
+++ b/aio.html
@@ -1315,7 +1315,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1519,7 +1519,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1852,7 +1852,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2154,7 +2154,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2173,7 +2173,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2467,7 +2467,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3644,7 +3644,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -3685,24 +3685,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3742,19 +3743,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3763,7 +3765,7 @@ MATLAB<
OUTPUT
@@ -3772,41 +3774,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3814,13 +3818,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3829,20 +3842,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3889,39 +3902,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3929,12 +3942,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3961,18 +3974,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -3992,17 +4005,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4059,47 +4072,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4136,42 +4149,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor-notes.html b/instructor-notes.html
index 82e7893..ca35423 100644
--- a/instructor-notes.html
+++ b/instructor-notes.html
@@ -405,7 +405,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/instructor/02-arrays.html b/instructor/02-arrays.html
index e0e69c5..16981f9 100644
--- a/instructor/02-arrays.html
+++ b/instructor/02-arrays.html
@@ -630,7 +630,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -832,7 +832,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
diff --git a/instructor/03-loading_data.html b/instructor/03-loading_data.html
index 27d4ee9..3728447 100644
--- a/instructor/03-loading_data.html
+++ b/instructor/03-loading_data.html
@@ -578,7 +578,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -876,7 +876,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -895,7 +895,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
diff --git a/instructor/04-plotting.html b/instructor/04-plotting.html
index 1488ba5..717c759 100644
--- a/instructor/04-plotting.html
+++ b/instructor/04-plotting.html
@@ -563,7 +563,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
diff --git a/instructor/07-func.html b/instructor/07-func.html
index 5dacebc..f9ad592 100644
--- a/instructor/07-func.html
+++ b/instructor/07-func.html
@@ -335,7 +335,7 @@
Creating Functions
- Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -378,24 +378,25 @@ Objectives
Writing functions from scratch
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -435,19 +436,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -456,7 +458,7 @@ MATLAB<
OUTPUT
@@ -465,41 +467,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -507,13 +511,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -522,20 +535,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -580,39 +593,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -620,12 +633,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -652,18 +665,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -683,17 +696,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -750,47 +763,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -825,42 +838,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -947,7 +960,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/instructor/aio.html b/instructor/aio.html
index f2a7fc7..f373fcb 100644
--- a/instructor/aio.html
+++ b/instructor/aio.html
@@ -1319,7 +1319,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1857,7 +1857,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/md5sum.txt b/md5sum.txt
index 8f43bdb..7217342 100644
--- a/md5sum.txt
+++ b/md5sum.txt
@@ -9,7 +9,7 @@
"episodes/04-plotting.md" "686b418946e7b164a392c060ad096705" "site/built/04-plotting.md" "2024-03-22"
"episodes/05-scripts.md" "56258cbb1e04bde1d86d974fa3f20e7c" "site/built/05-scripts.md" "2023-12-08"
"episodes/06-cond.md" "c8e7b50395e7d26ede321c9a23284726" "site/built/06-cond.md" "2023-12-04"
-"episodes/07-func.md" "4af772aa1e02408c453cdaabef404e3c" "site/built/07-func.md" "2023-12-04"
+"episodes/07-func.md" "950fb0b2819db046b3b546555c1813f7" "site/built/07-func.md" "2024-11-18"
"episodes/08-loops.md" "e00de44cbbcc50ab075fcb51f337953a" "site/built/08-loops.md" "2023-12-04"
"instructors/instructor-notes.md" "95bc9dd61103b3d6345f9d96dce4538b" "site/built/instructor-notes.md" "2023-10-18"
"learners/reference.md" "938a4e51e92ebb2e3cf8feb3fc932cd0" "site/built/reference.md" "2023-12-04"
diff --git a/pkgdown.yml b/pkgdown.yml
index d485019..27ab95d 100644
--- a/pkgdown.yml
+++ b/pkgdown.yml
@@ -2,4 +2,4 @@ pandoc: 3.1.11
pkgdown: 2.1.1
pkgdown_sha: ~
articles: {}
-last_built: 2024-11-18T14:28Z
+last_built: 2024-11-18T15:00Z
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
Plotting patient vs mean
-MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
Key Points
"url": "https://uomresearchit.github.io/matlab-novice/07-func.html", "identifier": "https://uomresearchit.github.io/matlab-novice/07-func.html", "dateCreated": "2014-12-12", - "dateModified": "2023-12-04", + "dateModified": "2024-11-18", "datePublished": "2024-11-18" } diff --git a/aio.html b/aio.html index 59145d4..1a681aa 100644 --- a/aio.html +++ b/aio.html @@ -1315,7 +1315,7 @@Checkerboard
-We need to select every other element in both dimensions. To do that, we define the apropriate intervals with an increment of 2:
@@ -1519,7 +1519,7 @@Master indexing
-We need to tart with row 2
, and subsequently select
every third row:
All data points at once
-We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2154,7 +2154,7 @@
Most inflamed patients
-Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2173,7 +2173,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2467,7 +2467,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3644,7 +3644,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -3685,24 +3685,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3742,19 +3743,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3763,7 +3765,7 @@ MATLAB<
OUTPUT
@@ -3772,41 +3774,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3814,13 +3818,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3829,20 +3842,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3889,39 +3902,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3929,12 +3942,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3961,18 +3974,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -3992,17 +4005,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4059,47 +4072,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4136,42 +4149,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor-notes.html b/instructor-notes.html
index 82e7893..ca35423 100644
--- a/instructor-notes.html
+++ b/instructor-notes.html
@@ -405,7 +405,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/instructor/02-arrays.html b/instructor/02-arrays.html
index e0e69c5..16981f9 100644
--- a/instructor/02-arrays.html
+++ b/instructor/02-arrays.html
@@ -630,7 +630,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -832,7 +832,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
diff --git a/instructor/03-loading_data.html b/instructor/03-loading_data.html
index 27d4ee9..3728447 100644
--- a/instructor/03-loading_data.html
+++ b/instructor/03-loading_data.html
@@ -578,7 +578,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -876,7 +876,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -895,7 +895,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
diff --git a/instructor/04-plotting.html b/instructor/04-plotting.html
index 1488ba5..717c759 100644
--- a/instructor/04-plotting.html
+++ b/instructor/04-plotting.html
@@ -563,7 +563,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
diff --git a/instructor/07-func.html b/instructor/07-func.html
index 5dacebc..f9ad592 100644
--- a/instructor/07-func.html
+++ b/instructor/07-func.html
@@ -335,7 +335,7 @@
Creating Functions
- Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -378,24 +378,25 @@ Objectives
Writing functions from scratch
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -435,19 +436,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -456,7 +458,7 @@ MATLAB<
OUTPUT
@@ -465,41 +467,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -507,13 +511,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -522,20 +535,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -580,39 +593,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -620,12 +633,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -652,18 +665,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -683,17 +696,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -750,47 +763,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -825,42 +838,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -947,7 +960,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/instructor/aio.html b/instructor/aio.html
index f2a7fc7..f373fcb 100644
--- a/instructor/aio.html
+++ b/instructor/aio.html
@@ -1319,7 +1319,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1857,7 +1857,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/md5sum.txt b/md5sum.txt
index 8f43bdb..7217342 100644
--- a/md5sum.txt
+++ b/md5sum.txt
@@ -9,7 +9,7 @@
"episodes/04-plotting.md" "686b418946e7b164a392c060ad096705" "site/built/04-plotting.md" "2024-03-22"
"episodes/05-scripts.md" "56258cbb1e04bde1d86d974fa3f20e7c" "site/built/05-scripts.md" "2023-12-08"
"episodes/06-cond.md" "c8e7b50395e7d26ede321c9a23284726" "site/built/06-cond.md" "2023-12-04"
-"episodes/07-func.md" "4af772aa1e02408c453cdaabef404e3c" "site/built/07-func.md" "2023-12-04"
+"episodes/07-func.md" "950fb0b2819db046b3b546555c1813f7" "site/built/07-func.md" "2024-11-18"
"episodes/08-loops.md" "e00de44cbbcc50ab075fcb51f337953a" "site/built/08-loops.md" "2023-12-04"
"instructors/instructor-notes.md" "95bc9dd61103b3d6345f9d96dce4538b" "site/built/instructor-notes.md" "2023-10-18"
"learners/reference.md" "938a4e51e92ebb2e3cf8feb3fc932cd0" "site/built/reference.md" "2023-12-04"
diff --git a/pkgdown.yml b/pkgdown.yml
index d485019..27ab95d 100644
--- a/pkgdown.yml
+++ b/pkgdown.yml
@@ -2,4 +2,4 @@ pandoc: 3.1.11
pkgdown: 2.1.1
pkgdown_sha: ~
articles: {}
-last_built: 2024-11-18T14:28Z
+last_built: 2024-11-18T15:00Z
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2467,7 +2467,7 @@
Patients 3 & 4
-The first part for the mean remains unchanged:
Key Points
Content from Creating Functions
-
Last updated on 2023-12-04 | +
Last updated on 2024-11-18 | Edit this page
Objectives
It has come to our attention that the data about inflammation that we’ve been analysing contains some systematic errors. The measurements -were made using the incorrect scale, with inflammation recorded in -Arbitrary Inflammation Units (AIU) rather than the scientific standard -International Inflmmation Units (IIU). Luckily there is a handy formula -which can be used for converting measurements in AIU to IIU, but it +were made using the incorrect scale, with inflammation recorded in units +of Swellocity (swell) rather than the scientific standard units of +Inflammatons (inf). Luckily there is a handy formula which can be used +for converting measurements in Swellocity to Inflammatons, but it involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
There are twelve files worth of data to be converted from AIU to IIU: -is there a way we can do this quickly and conveniently? If we have to -re-enter the conversion formula multiple times, the chance of us getting -the constants wrong is high. Thankfully there is a convenient way to -teach MATLAB how to do new things, like converting units from AIU to -IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity +to Inflammatons: is there a way we can do this quickly and conveniently? +If we have to re-enter the conversion formula multiple times, the chance +of us getting the constants wrong is high. Thankfully there is a +convenient way to teach MATLAB how to do new things, like converting +units from Swellocity to Inflammatons. We can do this by writing a +function.
We have already used some predefined MATLAB functions which we can pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a @@ -3742,19 +3743,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
MATLAB<
OUTPUT
@@ -3772,41 +3774,43 @@ OUTPUT<
OUTPUT
@@ -3772,41 +3774,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a -script can be thought of as automating the command line, with full -access to all variables in the base workspace, whereas a function has -its own separate workspace.
+script automates the command line, with full access to all variables in +the base workspace, whereas a function has its own separate +workspace.To be able to access variables from your workspace inside a function, you have to pass them in as inputs. To be able to save variables to your -workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them +as outputs.As with any operation, if we want to save the result, we need to assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
And we can see val_in_IIU
saved in our workspace.
And we can see val_in_inf
saved in our workspace.
OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
Writing your own conversion function
We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
For reference the conversion formula to take inflammation measured in +Swellocity to inflammation measured in Inflammatons is:
+Writing your own conversion function
-MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
Functions that work on arrays
needs. The function will take the variablepatient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB script!
@@ -3929,12 +3942,12 @@MATLAB< MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command line:
-OUTPUT
@@ -3961,18 +3974,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used inside the function.
If we now run our function in the command line, we get:
-OUTPUT
@@ -3992,17 +4005,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4059,47 +4072,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4136,42 +4149,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor-notes.html b/instructor-notes.html
index 82e7893..ca35423 100644
--- a/instructor-notes.html
+++ b/instructor-notes.html
@@ -405,7 +405,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/instructor/02-arrays.html b/instructor/02-arrays.html
index e0e69c5..16981f9 100644
--- a/instructor/02-arrays.html
+++ b/instructor/02-arrays.html
@@ -630,7 +630,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -832,7 +832,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
diff --git a/instructor/03-loading_data.html b/instructor/03-loading_data.html
index 27d4ee9..3728447 100644
--- a/instructor/03-loading_data.html
+++ b/instructor/03-loading_data.html
@@ -578,7 +578,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -876,7 +876,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -895,7 +895,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
diff --git a/instructor/04-plotting.html b/instructor/04-plotting.html
index 1488ba5..717c759 100644
--- a/instructor/04-plotting.html
+++ b/instructor/04-plotting.html
@@ -563,7 +563,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
diff --git a/instructor/07-func.html b/instructor/07-func.html
index 5dacebc..f9ad592 100644
--- a/instructor/07-func.html
+++ b/instructor/07-func.html
@@ -335,7 +335,7 @@
Creating Functions
- Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -378,24 +378,25 @@ Objectives
Writing functions from scratch
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -435,19 +436,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -456,7 +458,7 @@ MATLAB<
OUTPUT
@@ -465,41 +467,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -507,13 +511,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -522,20 +535,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -580,39 +593,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -620,12 +633,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -652,18 +665,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -683,17 +696,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -750,47 +763,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -825,42 +838,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -947,7 +960,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/instructor/aio.html b/instructor/aio.html
index f2a7fc7..f373fcb 100644
--- a/instructor/aio.html
+++ b/instructor/aio.html
@@ -1319,7 +1319,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1857,7 +1857,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/md5sum.txt b/md5sum.txt
index 8f43bdb..7217342 100644
--- a/md5sum.txt
+++ b/md5sum.txt
@@ -9,7 +9,7 @@
"episodes/04-plotting.md" "686b418946e7b164a392c060ad096705" "site/built/04-plotting.md" "2024-03-22"
"episodes/05-scripts.md" "56258cbb1e04bde1d86d974fa3f20e7c" "site/built/05-scripts.md" "2023-12-08"
"episodes/06-cond.md" "c8e7b50395e7d26ede321c9a23284726" "site/built/06-cond.md" "2023-12-04"
-"episodes/07-func.md" "4af772aa1e02408c453cdaabef404e3c" "site/built/07-func.md" "2023-12-04"
+"episodes/07-func.md" "950fb0b2819db046b3b546555c1813f7" "site/built/07-func.md" "2024-11-18"
"episodes/08-loops.md" "e00de44cbbcc50ab075fcb51f337953a" "site/built/08-loops.md" "2023-12-04"
"instructors/instructor-notes.md" "95bc9dd61103b3d6345f9d96dce4538b" "site/built/instructor-notes.md" "2023-10-18"
"learners/reference.md" "938a4e51e92ebb2e3cf8feb3fc932cd0" "site/built/reference.md" "2023-12-04"
diff --git a/pkgdown.yml b/pkgdown.yml
index d485019..27ab95d 100644
--- a/pkgdown.yml
+++ b/pkgdown.yml
@@ -2,4 +2,4 @@ pandoc: 3.1.11
pkgdown: 2.1.1
pkgdown_sha: ~
articles: {}
-last_built: 2024-11-18T14:28Z
+last_built: 2024-11-18T15:00Z
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs, so in the command line, we run it as:
-MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4059,47 +4072,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4136,42 +4149,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor-notes.html b/instructor-notes.html
index 82e7893..ca35423 100644
--- a/instructor-notes.html
+++ b/instructor-notes.html
@@ -405,7 +405,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/instructor/02-arrays.html b/instructor/02-arrays.html
index e0e69c5..16981f9 100644
--- a/instructor/02-arrays.html
+++ b/instructor/02-arrays.html
@@ -630,7 +630,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -832,7 +832,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
diff --git a/instructor/03-loading_data.html b/instructor/03-loading_data.html
index 27d4ee9..3728447 100644
--- a/instructor/03-loading_data.html
+++ b/instructor/03-loading_data.html
@@ -578,7 +578,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -876,7 +876,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -895,7 +895,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
diff --git a/instructor/04-plotting.html b/instructor/04-plotting.html
index 1488ba5..717c759 100644
--- a/instructor/04-plotting.html
+++ b/instructor/04-plotting.html
@@ -563,7 +563,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
diff --git a/instructor/07-func.html b/instructor/07-func.html
index 5dacebc..f9ad592 100644
--- a/instructor/07-func.html
+++ b/instructor/07-func.html
@@ -335,7 +335,7 @@
Creating Functions
- Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -378,24 +378,25 @@ Objectives
Writing functions from scratch
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -435,19 +436,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -456,7 +458,7 @@ MATLAB<
OUTPUT
@@ -465,41 +467,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -507,13 +511,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -522,20 +535,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -580,39 +593,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -620,12 +633,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -652,18 +665,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -683,17 +696,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -750,47 +763,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -825,42 +838,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -947,7 +960,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/instructor/aio.html b/instructor/aio.html
index f2a7fc7..f373fcb 100644
--- a/instructor/aio.html
+++ b/instructor/aio.html
@@ -1319,7 +1319,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1857,7 +1857,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
- 10 min for intro slides
diff --git a/md5sum.txt b/md5sum.txt
index 8f43bdb..7217342 100644
--- a/md5sum.txt
+++ b/md5sum.txt
@@ -9,7 +9,7 @@
"episodes/04-plotting.md" "686b418946e7b164a392c060ad096705" "site/built/04-plotting.md" "2024-03-22"
"episodes/05-scripts.md" "56258cbb1e04bde1d86d974fa3f20e7c" "site/built/05-scripts.md" "2023-12-08"
"episodes/06-cond.md" "c8e7b50395e7d26ede321c9a23284726" "site/built/06-cond.md" "2023-12-04"
-"episodes/07-func.md" "4af772aa1e02408c453cdaabef404e3c" "site/built/07-func.md" "2023-12-04"
+"episodes/07-func.md" "950fb0b2819db046b3b546555c1813f7" "site/built/07-func.md" "2024-11-18"
"episodes/08-loops.md" "e00de44cbbcc50ab075fcb51f337953a" "site/built/08-loops.md" "2023-12-04"
"instructors/instructor-notes.md" "95bc9dd61103b3d6345f9d96dce4538b" "site/built/instructor-notes.md" "2023-10-18"
"learners/reference.md" "938a4e51e92ebb2e3cf8feb3fc932cd0" "site/built/reference.md" "2023-12-04"
diff --git a/pkgdown.yml b/pkgdown.yml
index d485019..27ab95d 100644
--- a/pkgdown.yml
+++ b/pkgdown.yml
@@ -2,4 +2,4 @@ pandoc: 3.1.11
pkgdown: 2.1.1
pkgdown_sha: ~
articles: {}
-last_built: 2024-11-18T14:28Z
+last_built: 2024-11-18T15:00Z
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
Plotting patient vs mean
-MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
Suggested Timing
-- 10 min for intro slides diff --git a/instructor/02-arrays.html b/instructor/02-arrays.html index e0e69c5..16981f9 100644 --- a/instructor/02-arrays.html +++ b/instructor/02-arrays.html @@ -630,7 +630,7 @@
- 10 min for intro slides diff --git a/md5sum.txt b/md5sum.txt index 8f43bdb..7217342 100644 --- a/md5sum.txt +++ b/md5sum.txt @@ -9,7 +9,7 @@ "episodes/04-plotting.md" "686b418946e7b164a392c060ad096705" "site/built/04-plotting.md" "2024-03-22" "episodes/05-scripts.md" "56258cbb1e04bde1d86d974fa3f20e7c" "site/built/05-scripts.md" "2023-12-08" "episodes/06-cond.md" "c8e7b50395e7d26ede321c9a23284726" "site/built/06-cond.md" "2023-12-04" -"episodes/07-func.md" "4af772aa1e02408c453cdaabef404e3c" "site/built/07-func.md" "2023-12-04" +"episodes/07-func.md" "950fb0b2819db046b3b546555c1813f7" "site/built/07-func.md" "2024-11-18" "episodes/08-loops.md" "e00de44cbbcc50ab075fcb51f337953a" "site/built/08-loops.md" "2023-12-04" "instructors/instructor-notes.md" "95bc9dd61103b3d6345f9d96dce4538b" "site/built/instructor-notes.md" "2023-10-18" "learners/reference.md" "938a4e51e92ebb2e3cf8feb3fc932cd0" "site/built/reference.md" "2023-12-04" diff --git a/pkgdown.yml b/pkgdown.yml index d485019..27ab95d 100644 --- a/pkgdown.yml +++ b/pkgdown.yml @@ -2,4 +2,4 @@ pandoc: 3.1.11 pkgdown: 2.1.1 pkgdown_sha: ~ articles: {} -last_built: 2024-11-18T14:28Z +last_built: 2024-11-18T15:00Z
Checkerboard
-We need to select every other element in both dimensions. To do that, we define the apropriate intervals with an increment of 2:
@@ -832,7 +832,7 @@Master indexing
-We need to tart with row 2
, and subsequently select
every third row:
All data points at once
-We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -876,7 +876,7 @@
Most inflamed patients
-Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -895,7 +895,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
diff --git a/instructor/04-plotting.html b/instructor/04-plotting.html
index 1488ba5..717c759 100644
--- a/instructor/04-plotting.html
+++ b/instructor/04-plotting.html
@@ -563,7 +563,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
diff --git a/instructor/07-func.html b/instructor/07-func.html
index 5dacebc..f9ad592 100644
--- a/instructor/07-func.html
+++ b/instructor/07-func.html
@@ -335,7 +335,7 @@
Creating Functions
- Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
@@ -378,24 +378,25 @@ Objectives
Writing functions from scratch
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -435,19 +436,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -456,7 +458,7 @@ MATLAB<
OUTPUT
@@ -465,41 +467,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -507,13 +511,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -522,20 +535,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -580,39 +593,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -620,12 +633,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -652,18 +665,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -683,17 +696,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -750,47 +763,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -825,42 +838,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -947,7 +960,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/instructor/aio.html b/instructor/aio.html
index f2a7fc7..f373fcb 100644
--- a/instructor/aio.html
+++ b/instructor/aio.html
@@ -1319,7 +1319,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1857,7 +1857,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
diff --git a/instructor/04-plotting.html b/instructor/04-plotting.html
index 1488ba5..717c759 100644
--- a/instructor/04-plotting.html
+++ b/instructor/04-plotting.html
@@ -563,7 +563,7 @@
Patients 3 & 4
-The first part for the mean remains unchanged:
Creating Functions
-Last updated on 2023-12-04 | +
Last updated on 2024-11-18 | Edit this page
@@ -378,24 +378,25 @@Objectives
Writing functions from scratch
It has come to our attention that the data about inflammation that we’ve been analysing contains some systematic errors. The measurements -were made using the incorrect scale, with inflammation recorded in -Arbitrary Inflammation Units (AIU) rather than the scientific standard -International Inflmmation Units (IIU). Luckily there is a handy formula -which can be used for converting measurements in AIU to IIU, but it +were made using the incorrect scale, with inflammation recorded in units +of Swellocity (swell) rather than the scientific standard units of +Inflammatons (inf). Luckily there is a handy formula which can be used +for converting measurements in Swellocity to Inflammatons, but it involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
There are twelve files worth of data to be converted from AIU to IIU: -is there a way we can do this quickly and conveniently? If we have to -re-enter the conversion formula multiple times, the chance of us getting -the constants wrong is high. Thankfully there is a convenient way to -teach MATLAB how to do new things, like converting units from AIU to -IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity +to Inflammatons: is there a way we can do this quickly and conveniently? +If we have to re-enter the conversion formula multiple times, the chance +of us getting the constants wrong is high. Thankfully there is a +convenient way to teach MATLAB how to do new things, like converting +units from Swellocity to Inflammatons. We can do this by writing a +function.
We have already used some predefined MATLAB functions which we can pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a @@ -435,19 +436,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
MATLAB<
OUTPUT
@@ -465,41 +467,43 @@ OUTPUT<
OUTPUT
@@ -465,41 +467,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a -script can be thought of as automating the command line, with full -access to all variables in the base workspace, whereas a function has -its own separate workspace.
+script automates the command line, with full access to all variables in +the base workspace, whereas a function has its own separate +workspace.To be able to access variables from your workspace inside a function, you have to pass them in as inputs. To be able to save variables to your -workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them +as outputs.As with any operation, if we want to save the result, we need to assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
And we can see val_in_IIU
saved in our workspace.
And we can see val_in_inf
saved in our workspace.
OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
Writing your own conversion function
We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
For reference the conversion formula to take inflammation measured in +Swellocity to inflammation measured in Inflammatons is:
+Writing your own conversion function
-MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
Functions that work on arrays
needs. The function will take the variablepatient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB script!
@@ -620,12 +633,12 @@MATLAB< MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command line:
-OUTPUT
@@ -652,18 +665,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used inside the function.
If we now run our function in the command line, we get:
-OUTPUT
@@ -683,17 +696,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -750,47 +763,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -825,42 +838,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -947,7 +960,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/instructor/aio.html b/instructor/aio.html
index f2a7fc7..f373fcb 100644
--- a/instructor/aio.html
+++ b/instructor/aio.html
@@ -1319,7 +1319,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1857,7 +1857,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs, so in the command line, we run it as:
-MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -750,47 +763,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -825,42 +838,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
@@ -947,7 +960,7 @@ Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html",
"dateCreated": "2014-12-12",
- "dateModified": "2023-12-04",
+ "dateModified": "2024-11-18",
"datePublished": "2024-11-18"
}
diff --git a/instructor/aio.html b/instructor/aio.html
index f2a7fc7..f373fcb 100644
--- a/instructor/aio.html
+++ b/instructor/aio.html
@@ -1319,7 +1319,7 @@ Checkerboard
-
+
We need to select every other element in both dimensions. To do that,
we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@ Master indexing
-
+
We need to tart with row 2
, and subsequently select
every third row:
@@ -1857,7 +1857,7 @@ All data points at once
-
+
We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-
+
Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
Plotting patient vs mean
-MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
Key Points
"url": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html", "identifier": "https://uomresearchit.github.io/matlab-novice/instructor/07-func.html", "dateCreated": "2014-12-12", - "dateModified": "2023-12-04", + "dateModified": "2024-11-18", "datePublished": "2024-11-18" } diff --git a/instructor/aio.html b/instructor/aio.html index f2a7fc7..f373fcb 100644 --- a/instructor/aio.html +++ b/instructor/aio.html @@ -1319,7 +1319,7 @@Checkerboard
-We need to select every other element in both dimensions. To do that, we define the apropriate intervals with an increment of 2:
@@ -1523,7 +1523,7 @@Master indexing
-We need to tart with row 2
, and subsequently select
every third row:
All data points at once
-We already know that the colon operator as an index returns all the
elements, so patient_data(:)
will return a vector with all
@@ -2159,7 +2159,7 @@
Most inflamed patients
-Using the power MATLAB has to compare arrays, we can check which
patients have a max
equal to the global_max
.
@@ -2178,7 +2178,7 @@
MATLAB<
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-
+
The first part for the mean remains unchanged:
@@ -3652,7 +3652,7 @@ Key Points
Content from Creating Functions
-Last updated on 2023-12-04 |
+
Last updated on 2024-11-18 |
Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@ Objectives
It has come to our attention that the data about inflammation that
we’ve been analysing contains some systematic errors. The measurements
-were made using the incorrect scale, with inflammation recorded in
-Arbitrary Inflammation Units (AIU) rather than the scientific standard
-International Inflmmation Units (IIU). Luckily there is a handy formula
-which can be used for converting measurements in AIU to IIU, but it
+were made using the incorrect scale, with inflammation recorded in units
+of Swellocity (swell) rather than the scientific standard units of
+Inflammatons (inf). Luckily there is a handy formula which can be used
+for converting measurements in Swellocity to Inflammatons, but it
involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
-There are twelve files worth of data to be converted from AIU to IIU:
-is there a way we can do this quickly and conveniently? If we have to
-re-enter the conversion formula multiple times, the chance of us getting
-the constants wrong is high. Thankfully there is a convenient way to
-teach MATLAB how to do new things, like converting units from AIU to
-IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity
+to Inflammatons: is there a way we can do this quickly and conveniently?
+If we have to re-enter the conversion formula multiple times, the chance
+of us getting the constants wrong is high. Thankfully there is a
+convenient way to teach MATLAB how to do new things, like converting
+units from Swellocity to Inflammatons. We can do this by writing a
+function.
We have already used some predefined MATLAB functions which we can
pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a
@@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
+MATLAB to use our Swellocity to Inflammaton conversion formula. Create a
+file called inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
@@ -3772,7 +3774,7 @@ MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
+the workspace, you’ll notice one big difference. Although a variable
+called inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
-Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
+Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a
-script can be thought of as automating the command line, with full
-access to all variables in the base workspace, whereas a function has
-its own separate workspace.
+script automates the command line, with full access to all variables in
+the base workspace, whereas a function has its own separate
+workspace.
To be able to access variables from your workspace inside a function,
you have to pass them in as inputs. To be able to save variables to your
-workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them
+as outputs.
As with any operation, if we want to save the result, we need to
assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
-And we can see val_in_IIU
saved in our workspace.
+And we can see val_in_inf
saved in our workspace.
@@ -3823,13 +3827,22 @@ OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
@@ -3838,20 +3851,20 @@ Writing your own conversion function
-
+
-
+
MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
@@ -3898,39 +3911,39 @@ Functions that work on arrays
needs. The function will take the variable patient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-
+
MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB
script!
@@ -3938,12 +3951,12 @@ MATLAB<
MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command
line:
-
+
OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-
+
MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used
inside the function.
If we now run our function in the command line, we get:
-
+
OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
We can only do this because we had already calculated
per_patient_max
. However, there is another way of doing
@@ -2473,7 +2473,7 @@
Patients 3 & 4
-The first part for the mean remains unchanged:
Key Points
Content from Creating Functions
-
Last updated on 2023-12-04 | +
Last updated on 2024-11-18 | Edit this page
Estimated time: 65 minutes
@@ -3694,24 +3694,25 @@Objectives
It has come to our attention that the data about inflammation that we’ve been analysing contains some systematic errors. The measurements -were made using the incorrect scale, with inflammation recorded in -Arbitrary Inflammation Units (AIU) rather than the scientific standard -International Inflmmation Units (IIU). Luckily there is a handy formula -which can be used for converting measurements in AIU to IIU, but it +were made using the incorrect scale, with inflammation recorded in units +of Swellocity (swell) rather than the scientific standard units of +Inflammatons (inf). Luckily there is a handy formula which can be used +for converting measurements in Swellocity to Inflammatons, but it involves some hard to remember constants:
MATLAB
-inflammation_IIU = (inflammation_AIU + B)*A
+inflammation_in_inf = (inflammation_in_swell + B)*A
B = 5.634
A = 0.275
There are twelve files worth of data to be converted from AIU to IIU: -is there a way we can do this quickly and conveniently? If we have to -re-enter the conversion formula multiple times, the chance of us getting -the constants wrong is high. Thankfully there is a convenient way to -teach MATLAB how to do new things, like converting units from AIU to -IIU. We can do this by writing a function.
+There are twelve files worth of data to be converted from Swellocity +to Inflammatons: is there a way we can do this quickly and conveniently? +If we have to re-enter the conversion formula multiple times, the chance +of us getting the constants wrong is high. Thankfully there is a +convenient way to teach MATLAB how to do new things, like converting +units from Swellocity to Inflammatons. We can do this by writing a +function.
We have already used some predefined MATLAB functions which we can pass arguments to. How can we define our own?
A MATLAB function must be saved in a text file with a @@ -3751,19 +3752,20 @@
MATLAB<
scripts, we will put our source code files in the src
folder.
Let’s put this into practice to create a function that will teach
-MATLAB to use our AIU to IIU conversion formula. Create a file called
-inflammation_AIU_to_IIU.m
in the src
folder,
-enter the following function definition, and save the file:
inflammation_swell_to_inf.m
in the
+src
folder, enter the following function definition, and
+save the file:
MATLAB
-function inflammation_in_IIU = inflammation_AIU_to_IIU(inflammation_in_AIU)
- % INFLAMMATION_AIU_TO_IIU Convert inflammation mesured in AIU to inflammation measued in IIU.
+function inflammation_in_inf = inflammation_swell_to_inf(inflammation_in_swell)
+ % INFLAMMATION_SWELL_TO_INF Convert inflammation mesured in Swellocity to inflammation measured in Inflammatons.
A = 0.275;
B = 5.634;
- inflammation_in_IIU = (inflammation_in_AIU + B)*A;
+ inflammation_in_inf = (inflammation_in_swell + B)*A;
end
MATLAB<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
OUTPUT
@@ -3781,41 +3783,43 @@ OUTPUT<
We got the number we expected, and at first glance it seems like it
is almost the same as a script. However, if you look at the variables in
-the workspace, you’ll probably notice one big difference. Although a
-variable called inflammation_in_IIU
was defined in the
-function, it does not exist in our workspace.
inflammation_in_inf
was defined in the function, it
+does not exist in our workspace.
Lets have a look using the debugger to see what is happening.
When we pass a value, like 0.5
, to the function, it is
-assigned to the variable inflammation_in_AIU
so that it can
-be used in the body of the function. To return a value from the
+assigned to the variable inflammation_in_swell
so that it
+can be used in the body of the function. To return a value from the
function, we must assign that value to the variable
-inflammation_in_IIU
from our function definition line. What
-ever value inflammation_in_IIU
has when the
+inflammation_in_inf
from our function definition line. What
+ever value inflammation_in_inf
has when the
end
keyword in the function definition is reached, that
will be the value returned.
Outside the function, the variables inflammation_in_AIU
,
-inflammation_in_IIU
, A
, and B
-aren’t accessible; they are only used by in function body.
Outside the function, the variables
+inflammation_in_swell
, inflammation_in_inf
,
+A
, and B
aren’t accessible; they are only used
+by in function body.
This is one of the major differences between scripts and functions: a -script can be thought of as automating the command line, with full -access to all variables in the base workspace, whereas a function has -its own separate workspace.
+script automates the command line, with full access to all variables in +the base workspace, whereas a function has its own separate +workspace.To be able to access variables from your workspace inside a function, you have to pass them in as inputs. To be able to save variables to your -workspace, it needs to return them as outputs.
+workspace from inside your function, the function needs to return them +as outputs.As with any operation, if we want to save the result, we need to assign the result to a variable, for example:
MATLAB
->> val_in_IIU = inflammation_AIU_to_IIU(0.5)
+>> val_in_inf = inflammation_swell_to_inf(0.5)
OUTPUT
-val_in_IIU = 1.6869
+val_in_inf = 1.6869
And we can see val_in_IIU
saved in our workspace.
And we can see val_in_inf
saved in our workspace.
OUTPUT<
Writing your own conversion function
-We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
+We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
+For reference the conversion formula to take inflammation measured in
+Swellocity to inflammation measured in Inflammatons is:
+
Writing your own conversion function
We’d like a function that reverses the conversion of AIU to IIU.
-Re-arrange the conversion formula and write a function called
-inflammation_IIU_to_AIU
that converts inflammation measued
-in IIU to inflammation measured in AIU.
We’d like a function that reverses the conversion of Swellocity to
+Inflammatons. Re-arrange the conversion formula and write a function
+called inflammation_inf_to_swell
that converts inflammation
+measued in Inflammatons to inflammation measured in Swellocity.
Remember to save your function definition in a file with the required
name, start the file with the function definition line, followed by the
function body, ending with the end
keyword.
For reference the conversion formula to take inflammation measured in +Swellocity to inflammation measured in Inflammatons is:
+Writing your own conversion function
-MATLAB
-function inflammation_in_AIU = inflammation_IIU_to_AIU(inflammation_in_IIU)
- % INFLAMMTION_IIU_TO_AIU Convert inflammation measured in IIU to inflammation measured in AIU.
-
- A = 0.275;
- B = 5.634;
-
- inflammation_in_AIU = inflammation_in_IIU/A - B;
-
-end
+function inflammation_in_swell = inflammation_inf_to_swell(inflammation_in_inf)
+ % INFLAMMTION_INF_TO_SWELL Convert inflammation measured in Inflammatons to inflammation measured in Swellocity.
+
+ A = 0.275;
+ B = 5.634;
+
+ inflammation_in_swell = inflammation_in_inf/A - B;
+
+end
Functions that work on arrays
needs. The function will take the variablepatient_number
as input and since we removed the line that assigned a value to that
variable, the input will decide which patient is analysed.
-MATLAB
-function patient_analysis(patient_number)
- % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
- % Takes the patient number as an input, and prints the relevant information to console.
- % Sample usage:
- % patient_analysis(5)
-
- % Load patient data
- patient_data = readmatrix('data/base/inflammation-01.csv');
-
- % Compute global statistics
- g_mean = mean(patient_data(:));
- g_max = max(patient_data(:));
- g_min = min(patient_data(:));
-
- % Compute patient statistics
- p_mean = mean(patient_data(patient_number,:));
- p_max = max(patient_data(patient_number,:));
- p_min = min(patient_data(patient_number,:));
-
- % Compare patient vs global
- disp('Patient:')
- disp(patient_number)
- disp('High mean?')
- disp(p_mean > g_mean)
- disp('Highest max?')
- disp(p_max == g_max)
- disp('Lowest min?')
- disp(p_min == g_min)
-
-end
+function patient_analysis(patient_number)
+ % PATIENT_ANALYSIS Computes mean, max and min of a patient and compares to global statistics.
+ % Takes the patient number as an input, and prints the relevant information to console.
+ % Sample usage:
+ % patient_analysis(5)
+
+ % Load patient data
+ patient_data = readmatrix('data/base/inflammation-01.csv');
+
+ % Compute global statistics
+ g_mean = mean(patient_data(:));
+ g_max = max(patient_data(:));
+ g_min = min(patient_data(:));
+
+ % Compute patient statistics
+ p_mean = mean(patient_data(patient_number,:));
+ p_max = max(patient_data(patient_number,:));
+ p_min = min(patient_data(patient_number,:));
+
+ % Compare patient vs global
+ disp('Patient:')
+ disp(patient_number)
+ disp('High mean?')
+ disp(p_mean > g_mean)
+ disp('Highest max?')
+ disp(p_max == g_max)
+ disp('Lowest min?')
+ disp(p_min == g_min)
+
+end
Congratulations! You’ve now created a MATLAB function from a MATLAB script!
@@ -3938,12 +3951,12 @@MATLAB< MATLAB does not need this, but it makes it much more readable!
Lets clear our workspace and run our function in the command line:
-OUTPUT
@@ -3970,18 +3983,18 @@ OUTPUT<
save it in p_mean
, but we need to tell MATLAB that we want
the function to return it.
p_mean
, but we need to tell MATLAB that we want
the function to return it.
To do that we modify the function definition like this:
-MATLAB
-function p_mean = patient_analysis(patient_number)
+function p_mean = patient_analysis(patient_number)
It is important that the variable name is the same that is used inside the function.
If we now run our function in the command line, we get:
-OUTPUT
@@ -4001,17 +4014,17 @@ OUTPUT<
min and max as well. To do that, we need to specify all the outputs in
square brackets, as an array. So we need to replace the function
definition for:
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs,
so in the command line, we run it as:
-
+
MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
MATLAB
-function [p_mean,p_max,p_min] = patient_analysis(patient_number)
+function [p_mean,p_max,p_min] = patient_analysis(patient_number)
To call our function now we need to provide space for all 3 outputs, so in the command line, we run it as:
-MATLAB
-[p13_mean,p13_max,p13_min] = patient_analysis(13)
+[p13_mean,p13_max,p13_min] = patient_analysis(13)
OUTPUT
@@ -4068,47 +4081,47 @@ Plotting daily average of different data files
-
+
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
@@ -4145,42 +4158,42 @@ Plotting patient vs mean
-
+
-
+
MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end
diff --git a/instructor/instructor-notes.html b/instructor/instructor-notes.html
index 590b5ad..e662e1f 100644
--- a/instructor/instructor-notes.html
+++ b/instructor/instructor-notes.html
@@ -407,7 +407,7 @@
Suggested Timing
-
+
MATLAB
-function plot_daily_average(data_file,plot_name)
- %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
- % The function takes the data in data_file and saves it as plot_name
- % Example usage:
- % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
-
- % Load patient data
- patient_data = readmatrix(data_file);
-
- figure(visible='off')
-
- % Define tiled layout and labels
- tlo = tiledlayout(1,3);
- xlabel(tlo,'Day of trial')
- ylabel(tlo,'Inflammation')
-
- % Plot average inflammation per day
- nexttile
- plot(mean(patient_data, 1))
- title('Average')
-
- % Plot max inflammation per day
- nexttile
- plot(max(patient_data, [], 1))
- title('Max')
-
- % Plot min inflammation per day
- nexttile
- plot(min(patient_data, [], 1))
- title('Min')
-
- % Save plot in 'results' folder as png image:
- saveas(gcf,plot_name)
-
- close()
-end
+function plot_daily_average(data_file,plot_name)
+ %PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.
+ % The function takes the data in data_file and saves it as plot_name
+ % Example usage:
+ % plot_daily_average('data/base/inflammation-03.csv','results/plot3.png')
+
+ % Load patient data
+ patient_data = readmatrix(data_file);
+
+ figure(visible='off')
+
+ % Define tiled layout and labels
+ tlo = tiledlayout(1,3);
+ xlabel(tlo,'Day of trial')
+ ylabel(tlo,'Inflammation')
+
+ % Plot average inflammation per day
+ nexttile
+ plot(mean(patient_data, 1))
+ title('Average')
+
+ % Plot max inflammation per day
+ nexttile
+ plot(max(patient_data, [], 1))
+ title('Max')
+
+ % Plot min inflammation per day
+ nexttile
+ plot(min(patient_data, [], 1))
+ title('Min')
+
+ % Save plot in 'results' folder as png image:
+ saveas(gcf,plot_name)
+
+ close()
+end
Plotting patient vs mean
-MATLAB
-function patient_vs_mean(per_day_mean,patient_data,patient_reference)
- % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
- % per_day_mean should be a vector with the global mean.
- % patient_data should be a vector with only the patient data.
- % patient_reference will be used to identify the patient on the plot.
- %
- % Sample usage:
- % patient_data = readmatrix('data/base/inflammation-01.csv');
- % per_day_mean = mean(patient_data);
- % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
-
- figure(visible='off')
-
- %Plot per_day_mean
- plot(per_day_mean,DisplayName="Mean")
- legend
- title('Daily average inflammation')
- xlabel('Day of trial')
- ylabel('Inflammation')
-
- %Overlap patient data
- hold on
- plot(patient_data,DisplayName=patient_reference)
- hold off
-
- % Save plot
- saveas(gcf,"results/"+patient_reference+".png")
-
- close()
-
-end
+function patient_vs_mean(per_day_mean,patient_data,patient_reference)
+ % PATIENT_VS_MEAN Plots the global mean and patient inflammation on top of each other.
+ % per_day_mean should be a vector with the global mean.
+ % patient_data should be a vector with only the patient data.
+ % patient_reference will be used to identify the patient on the plot.
+ %
+ % Sample usage:
+ % patient_data = readmatrix('data/base/inflammation-01.csv');
+ % per_day_mean = mean(patient_data);
+ % patient_vs_mean(per_day_mean,patient_data(5,:),"Patient 5")
+
+ figure(visible='off')
+
+ %Plot per_day_mean
+ plot(per_day_mean,DisplayName="Mean")
+ legend
+ title('Daily average inflammation')
+ xlabel('Day of trial')
+ ylabel('Inflammation')
+
+ %Overlap patient data
+ hold on
+ plot(patient_data,DisplayName=patient_reference)
+ hold off
+
+ % Save plot
+ saveas(gcf,"results/"+patient_reference+".png")
+
+ close()
+
+end