Skip to content

Commit

Permalink
Extracted code and fixed " vs ' and >> issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fherreazcue committed Dec 6, 2023
1 parent 99cdbf3 commit 254c768
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 89 deletions.
2 changes: 1 addition & 1 deletion code/02-arrays.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
% ## Initializing an Array
Z = zeros(3,5)
Z = zeros(3)
Z = zeros(1x5)
Z = zeros(1,5)
R = rand(8);
O = ones(10,10);

Expand Down
5 changes: 5 additions & 0 deletions code/03-loading_data.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
% ## Most inflamed patients
% !! Solution:
find(per_patient_max == global_max)
% !! Solution:
% ## Alternative solution
find(patient_data == global_max)
[r,c]=ind2sub(size(patient_data),find(patient_data == global_max))




Expand Down
74 changes: 41 additions & 33 deletions code/04-plotting.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@



title('Daily average inflammation')
xlabel('Day of trial')
ylabel('Inflammation')
title("Daily average inflammation")
xlabel("Day of trial")
ylabel("Inflammation")
plot(per_day_max)
title('Maximum inflammation per day')
ylabel('Inflammation')
xlabel('Day of trial')
title("Maximum inflammation per day")
ylabel("Inflammation")
xlabel("Day of trial")

% ## Scripts
edit src/single_plot.m
addpath("src")

plot(per_day_min)
title('Minimum inflammation per day')
ylabel('Inflammation')
xlabel('Day of trial')
title("Minimum inflammation per day")
ylabel("Inflammation")
xlabel("Day of trial")
% ## Multiple lines in a plot
copyfile("src/single_plot.m","src/multiline_plot.m")
plot(per_day_mean,DisplayName="Mean")
legend
title('Daily average inflammation')
xlabel('Day of trial')
ylabel('Inflammation')
title("Daily average inflammation")
xlabel("Day of trial")
ylabel("Inflammation")
hold on
plot(patient_5,DisplayName="Patient 5")
hold off
Expand All @@ -34,37 +40,38 @@
% !! Solution:
plot(per_day_mean,DisplayName="Mean")
legend
title('Daily average inflammation')
xlabel('Day of trial')
ylabel('Inflammation')
title("Daily average inflammation")
xlabel("Day of trial")
ylabel("Inflammation")
hold on
plot(patient_data(3,:),DisplayName="Patient 3")
plot(patient_data(4,:),DisplayName="Patient 4")
hold off


% ## Multiple plots in a figure
edit src/tiled_plot.m
tiledlayout(1, 2)
nexttile
plot(per_day_max)
title('Max')
xlabel('Day of trial')
ylabel('Inflamation')
title("Max")
xlabel("Day of trial")
ylabel("Inflamation")
nexttile
plot(per_day_min)
title('Min')
xlabel('Day of trial')
ylabel('Inflamation')
tlo=tiledlayout(1, 2)
title(tlo,'Per day data')
xlabel(tlo,'Day of trial')
ylabel(tlo,'Inflamation')
title("Min")
xlabel("Day of trial")
ylabel("Inflamation")
tlo=tiledlayout(1, 2);
title(tlo,"Per day data")
xlabel(tlo,"Day of trial")
ylabel(tlo,"Inflamation")
nexttile
plot(per_day_max)
title('Max')
title("Max")
nexttile
plot(per_day_min)
title('Min')
title("Min")

% ## Where is the `nexttile`?
tiledlayout(3,5)
Expand All @@ -78,15 +85,16 @@


% ## Clearing a figure
clf

% ## Heatmaps
heatmap(patient_data)
title('Inflammation')
xlabel('Day of trial')
ylabel('Patient number')
title("Inflammation")
xlabel("Day of trial")
ylabel("Patient number")
imagesc(patient_data)
title('Inflammation')
xlabel('Day of trial')
ylabel('Patient number')
title("Inflammation")
xlabel("Day of trial")
ylabel("Patient number")


86 changes: 38 additions & 48 deletions code/05-scripts.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
% ## The MATLAB path

% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
patient_data = readmatrix("data/base/inflammation-01.csv");

% Compute global statistics
g_mean = mean(patient_data(:));
Expand All @@ -20,12 +20,12 @@
p_min = min(patient_data(5,:));

% Compare patient vs global
disp('Patient 5:')
disp('High mean?')
disp("Patient 5:")
disp("High mean?")
disp(p_mean > g_mean)
disp('Highest max?')
disp("Highest max?")
disp(p_max == g_max)
disp('Lowest min?')
disp("Lowest min?")
disp(p_min == g_min)

% ## Comments
Expand All @@ -34,28 +34,27 @@
clc
patient_analysis
% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
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(:));

patient_number = 8;

% Compute patient statistics
patient_number = 8;
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:")
disp(patient_number)
disp('High mean?')
disp("High mean?")
disp(p_mean > g_mean)
disp('Highest max?')
disp("Highest max?")
disp(p_max == g_max)
disp('Lowest min?')
disp("Lowest min?")
disp(p_min == g_min)
patient_analysis

Expand All @@ -65,83 +64,74 @@
help patient_analysis
% ## Script for plotting
edit src/plot_daily_average.m
% PLOT_DAILY_AVERAGE Plots daily average inflammation accross patients.

% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');

figure

% Plot average inflammation per day
plot(mean(patient_data, 1))
title('Daily average inflammation')
xlabel('Day of trial')
ylabel('Inflammation')
figure
plot_daily_average
% ### Modified script for sub-plots
% PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.

% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
patient_data = readmatrix("data/base/inflammation-01.csv");

figure
fig = figure;

% Define tiled layout and labels
tlo = tiledlayout(1,3);
xlabel(tlo,'Day of trial')
ylabel(tlo,'Inflammation')
xlabel(tlo,"Day of trial")
ylabel(tlo,"Inflammation")

% Plot average inflammation per day
nexttile
plot(mean(patient_data, 1))
title('Average')
title("Average")

% Plot max inflammation per day
nexttile
plot(max(patient_data, [], 1))
title('Max')
title("Max")

% Plot min inflammation per day
nexttile
plot(min(patient_data, [], 1))
title('Min')
% Save plot in 'results' folder as png image:
print('results/daily_average_01','-dpng')
title("Min")
figure
plot_daily_average
% ### Saving figures
% Save plot in "results" folder as png image:
saveas(fig,"results/daily_average_01.png")

% Save plot in 'results' folder as png image:
saveas(gcf,'results/daily_average_01.png')
% ## Getting the current figure
% Save plot in "results" folder as png image:
saveas(gcf,"results/daily_average_01.png")
gcf == fig

% ### Hiding figures
% PLOT_DAILY_AVERAGE Saves plot of daily average, max and min inflammation accross patients.

% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
patient_data = readmatrix("data/base/inflammation-01.csv");

figure(visible='off')
fig = figure(Visible='off');

% Define tiled layout and labels
tlo = tiledlayout(1,3);
xlabel(tlo,'Day of trial')
ylabel(tlo,'Inflammation')
xlabel(tlo,"Day of trial")
ylabel(tlo,"Inflammation")

% Plot average inflammation per day
nexttile
plot(mean(patient_data, 1))
title('Average')
title("Average")

% Plot max inflammation per day
nexttile
plot(max(patient_data, [], 1))
title('Max')
title("Max")

% Plot min inflammation per day
nexttile
plot(min(patient_data, [], 1))
title('Min')
title("Min")

% Save plot in 'results' folder as png image:
saveas(gcf,'results/daily_average_01.png')
% Save plot in "results" folder as png image:
saveas(fig,"results/daily_average_01.png")

close()
close(fig)


2 changes: 1 addition & 1 deletion episodes/02-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Z =
yields a 3×3 array.
If we want a single row and 5 columns, we need to remember that MATLAB reads `rows`×`columns`, so
```matlab
>> Z = zeros(1x5)
>> Z = zeros(1,5)
```
```output
Z =
Expand Down
2 changes: 1 addition & 1 deletion episodes/03-loading_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ However, there is another way of doing this.
Just as we used `find` to locate the patients that had a maximum inflammation value equal to the global maximum,
we can find the value from the whole data set:
```matlab
find(patient_data == global_max)
>> find(patient_data == global_max)
```
```
ans =
Expand Down
14 changes: 9 additions & 5 deletions episodes/04-plotting.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ it creates the file with the correct path and name for you.
so that MATLAB knows where to find the script.
To do that, right click on the `src` directory, go to "Add to Path" and to "Selected Folders". Alternatively, run:
```matlab
addpath("src")
>> addpath("src")
```

Try copying and pasting the plot commands for the max inflammation on the script and clicking on the "Run" button!
Expand All @@ -122,7 +122,7 @@ Because we now have a script, it should be much easier to change the plot to the
>> xlabel("Day of trial")
```

![](fig/plotting_min-inflammation.svg){alt="Minumum inflammation"}
![](fig/plotting_min-inflammation.svg){alt='Minumum inflammation'}

These two are much more noisy than the mean, as we'd be expect.

Expand Down Expand Up @@ -219,7 +219,7 @@ To be able to plot something on each of the tiles, we use the [`nexttile`](https

Lets start a new script for this topic:
```matlab
edit src/tiled_plot.m
>> edit src/tiled_plot.m
```
We can show the average daily min and max plots together with:
```matlab
Expand All @@ -240,7 +240,7 @@ We can show the average daily min and max plots together with:
We can also specify titles and labels for the whole tiled layout if we assign the tiled layout to a variable
and pass it as a first argument to `title`, `xlabel` or `ylabel`, for example:
```matlab
>> tlo=tiledlayout(1, 2)
>> tlo=tiledlayout(1, 2);
>> title(tlo,"Per day data")
>> xlabel(tlo,"Day of trial")
>> ylabel(tlo,"Inflamation")
Expand Down Expand Up @@ -294,7 +294,11 @@ Note that using a starting tile that overlaps another plot will erase that axes.
If you now try to plot something like the mean, as we had done before,
you will notice that the plot is assigned to the second plot space in the tiled layout.

To clear the tiled layout, you can use the instruction `clf`, which stands for "clear figure".
To clear the tiled layout, you can use the instruction
```matlab
>> clf
```
which stands for "clear figure".

::::::::::::::::::::::::::::::::::::::::::::::::::

Expand Down

0 comments on commit 254c768

Please sign in to comment.