From 254c768f272708b93778daf895dd7e97bea1bb8d Mon Sep 17 00:00:00 2001 From: fherreazcue Date: Wed, 6 Dec 2023 16:13:09 +0000 Subject: [PATCH] Extracted code and fixed " vs ' and >> issues --- code/02-arrays.m | 2 +- code/03-loading_data.m | 5 +++ code/04-plotting.m | 74 +++++++++++++++++-------------- code/05-scripts.m | 86 ++++++++++++++++--------------------- episodes/02-arrays.md | 2 +- episodes/03-loading_data.md | 2 +- episodes/04-plotting.md | 14 +++--- 7 files changed, 96 insertions(+), 89 deletions(-) diff --git a/code/02-arrays.m b/code/02-arrays.m index e64bfa19..f164ddb6 100644 --- a/code/02-arrays.m +++ b/code/02-arrays.m @@ -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); diff --git a/code/03-loading_data.m b/code/03-loading_data.m index 1d3f24a5..1fc7b3a6 100644 --- a/code/03-loading_data.m +++ b/code/03-loading_data.m @@ -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)) + diff --git a/code/04-plotting.m b/code/04-plotting.m index 3716aa6a..429426ba 100644 --- a/code/04-plotting.m +++ b/code/04-plotting.m @@ -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 @@ -34,9 +40,9 @@ % !! 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") @@ -44,27 +50,28 @@ % ## 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) @@ -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") diff --git a/code/05-scripts.m b/code/05-scripts.m index 38440421..08337fb6 100644 --- a/code/05-scripts.m +++ b/code/05-scripts.m @@ -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(:)); @@ -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 @@ -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 @@ -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) diff --git a/episodes/02-arrays.md b/episodes/02-arrays.md index 182377fd..ef70513e 100644 --- a/episodes/02-arrays.md +++ b/episodes/02-arrays.md @@ -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 = diff --git a/episodes/03-loading_data.md b/episodes/03-loading_data.md index 1ecfd782..cc88fd60 100644 --- a/episodes/03-loading_data.md +++ b/episodes/03-loading_data.md @@ -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 = diff --git a/episodes/04-plotting.md b/episodes/04-plotting.md index ba92af5b..c8889a55 100644 --- a/episodes/04-plotting.md +++ b/episodes/04-plotting.md @@ -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! @@ -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. @@ -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 @@ -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") @@ -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". ::::::::::::::::::::::::::::::::::::::::::::::::::