Clear Filters
Clear Filters

PLOT LINEAR TREND, MEAN PLOT

6 views (last 30 days)
Willian
Willian on 14 May 2024
Commented: Willian on 15 May 2024
Good Evening,
How can I graph the average value of the data indicated in the attached file, as well as its linear trend and simulated pressure and simulated trend, as indicated in the following graph
My code in attached file
clear;
clc;
%% data example 01
filename = 'example1'; %abrir archivo
T = readtable(filename) ;%ignore the warning about modified column names
x13202412 = T{:,1} + days(T{:,2}); %formato fecha y hora en dos columnas (1era y 2da columna)
y13202412 = T{:,3};%(datos tercera columna)
plot(x13202412,[y13202412],'r-','lineWidth',1)
hold on
%% data example 02
filename = 'example2'; %abrir archivo
T = readtable(filename) ;%ignore the warning about modified column names
x23202412 = T{:,1} + days(T{:,2}); %formato fecha y hora en dos columnas (1era y 2da columna)
y23202412 = T{:,3};%(datos tercera columna)
plot(x23202412,[y23202412],'r-','lineWidth',1)
hold on
%%
%figure('Name','Measured Data');
xlabel('time (hours)')
ylabel('pressure')
title('DATA')
legend({'-'},'Location','southeast');
grid on
grid minor

Accepted Answer

Nipun
Nipun on 14 May 2024
Hi Willian,
I understand that you are want to plot the average as well as the linear trend of a given set of data points. I assume that you have two sets of data from example1 and example2 files and you want to add simulated data and trends for both.
To plot the average of the data points, you may use the "movmean" MATLAB function to calculate the moving average of the data. Based on the illustrated use case, you may set the window size as 10 and plot the corresponding data. For syntax related information for "movmean" function, refer to the following MathWorks documentation:
I assume that you intend to data from example1 and example2 variables. Here is an example code snippet that illustrates this use case:
clear;
clc;
%% Load and plot data example 01
filename = 'example1';
T = readtable(filename);
x13202412 = T{:,1} + days(T{:,2}); % Combining date and time
y13202412 = T{:,3}; % Pressure data
plot(x13202412, y13202412, 'r-', 'LineWidth', 1)
hold on
%% Load and plot data example 02
filename = 'example2';
T = readtable(filename);
x23202412 = T{:,1} + days(T{:,2}); % Combining date and time
y23202412 = T{:,3}; % Pressure data
plot(x23202412, y23202412, 'b-', 'LineWidth', 1)
hold on
%% Calculating and plotting moving averages
windowSize = 10; % Window size for moving average
movAvgY1 = movmean(y13202412, windowSize);
movAvgY2 = movmean(y23202412, windowSize);
plot(x13202412, movAvgY1, 'r--', 'LineWidth', 1);
plot(x23202412, movAvgY2, 'b--', 'LineWidth', 1);
I recommend using the "polyfit" function in MATLAB to plot the linear trend for the given data set. The "ployfit" functions returns a best fit curve that fits a series of data. Refer to the following MathWorks documentation for more information on fitting a set of points to a curve in MATLAB:
Here is a example code snippet illustrating use case of "polyfit" function to create linear trends:
%% Calculating and plotting linear trends
coeffs1 = polyfit(datenum(x13202412), y13202412, 1);
coeffs2 = polyfit(datenum(x23202412), y23202412, 1);
plot(x13202412, polyval(coeffs1, datenum(x13202412)), 'k--', 'LineWidth', 1);
plot(x23202412, polyval(coeffs2, datenum(x23202412)), 'k--', 'LineWidth', 1);
%% Simulated pressure data (example)
% Assuming simulated data follows a similar structure
simX1 = x13202412;
simY1 = y13202412 + randn(size(y13202412)) * 5; % Adding noise for simulation
plot(simX1, simY1, 'g.', 'LineWidth', 1);
simX2 = x23202412;
simY2 = y23202412 + randn(size(y23202412)) * 5; % Adding noise for simulation
plot(simX2, simY2, 'm.', 'LineWidth', 1);
%% Calculating and plotting simulated trends
simCoeffs1 = polyfit(datenum(simX1), simY1, 1);
simCoeffs2 = polyfit(datenum(simX2), simY2, 1);
plot(simX1, polyval(simCoeffs1, datenum(simX1)), 'g--', 'LineWidth', 1);
plot(simX2, polyval(simCoeffs2, datenum(simX2)), 'm--', 'LineWidth', 1);
Hope this helps.
Regards,
Nipun
  1 Comment
Willian
Willian on 15 May 2024
Thank you very much, you have helped me clear up some questions

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!