Boxplot and mean for selected range of temporal datasets
7 views (last 30 days)
Show older comments
Hello there,
I have a datasets containing 3 parameters, let's say z_i as depth, HOUR as time (in numeric format), and EP as the parameter I want to check its variability over depth and time. My intention is to get the EP median, min-max, and mean for selected range of z_i for each time, and then plot them all in a box plot and mean (in one plot), with x-axis as time and y-plot is the EP (better plotted in log-10 scale as the difference is very small).
See my file attached and the illustration of the flow process as I described above below:

Thank you!
1 Comment
Answers (1)
Shishir Reddy
on 6 Nov 2024
Hi Adi
As per my understanding, you would like to filter a dataset based on a specified depth range (‘z_i’) and calculate statistical measures (median, min, max, mean) of the parameter ‘EP’ for each hour (‘HOUR’).
From the ‘data_w.mat’ file, I see that ‘EP’ and ‘HOUR’ are matrices with 6 rows (possibly representing different observations or repetitions) and 9991 columns , while ‘z_i’ is a vector with 9991 elements, representing depth.
You can process this data in MATLAB as follows –
1. Load the data
load('data_w.mat'); % This loads EP, HOUR, and z_i
2. Filter the data
filtered_indices = (z_i >= z_min) & (z_i <= z_max); % z_min, z_max, depends on the selected range
filtered_EP = EP(:, filtered_indices);
filtered_HOUR = HOUR(:, filtered_indices);
filtered_EP = filtered_EP(:);% Flattening the matrices
filtered_HOUR = filtered_HOUR(:);
3. Compute the statistics
% Assumption – ‘unique_hours’, ‘medians’, ‘mins’, ‘maxs’, ‘means’ are declared.
for i = 1:length(unique_hours)
hour_data = filtered_EP(filtered_HOUR == unique_hours(i));
medians(i) = median(hour_data);
mins(i) = min(hour_data);
maxs(i) = max(hour_data);
means(i) = mean(hour_data);
end
4. Plotting
figure;
hold on;
boxplot(filtered_EP, filtered_HOUR, 'Colors', [0.7 0.7 0.7], 'Symbol', '');
plot(unique_hours + 1, means, 'ro-', 'LineWidth', 1.5, 'DisplayName', 'Mean');
set(gca, 'YScale', 'log');
For more information regarding the ‘boxplot’ function, kindly refer to the following documentation - https://www.mathworks.com/help/stats/boxplot.html
I hope this helps.
8 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
