Plotting the average line of a graph of a column imported from an excel

7 views (last 30 days)
I have an imported table from an excel file of a large column with 15808 rows, I had successfully plotted it but I'd also like to plot an average line of these whole values. I tried many average functions and checked many similar questions on the website but that didn't really help. I also calculated the average of the column with its length and its sum (the code below, while x is the clumn of the values in the workspace) but the line stays at 0..
Is there any suggested function how to plot the average of a large column with values?
len = length(x);
sum = 0;
for i = 1 : len
sum = sum + x(i);
end
Avg = sum / len;

Accepted Answer

Sudheer Bhimireddy
Sudheer Bhimireddy on 7 Aug 2020
Not sure, where you are getting the error.
I tried this
% some random numbers
x=rand(15808,1);
% get the average by using mean function
% NOTE: If you have NaN values inside x, then you have to use nanmean to get the average
x_avg = nanmean(x);
h=figure(1);
clf;
hold on;
plot(x,'k.');
plot([0 15808],[x_avg x_avg],'r-','LineWidth',2);
and got this
Hope this helps.
  7 Comments
Ramo Rafsel
Ramo Rafsel on 12 Aug 2020
Edited: Ramo Rafsel on 12 Aug 2020
Now this code below works perfectly, i just want to plot both figures in one but it doesn't work. And the second problem that I couldn't solve is that I want to make the periods of time (Format HH:MM:SS) somehow longer so that I can see the time in x-axis (see the attached picture), for example through choosing specific time cells that can be displayed on the x axis.
@Sudheer Bhimireddy Thanks a lot in advance for your help.
v5=variable5.value; %whereas variable5 is the imported excel file
time5=variable5.time; %and value and time are the two column where time is for the x axis and value for the y axis
avg5 = sum(v5)/size(v5,1); %the average line of the column v5
nexttile
plot(time5,value5);
title('value 5')
nexttile
plot( [0 size(v5,1)],[avg5 avg5], 'y-', 'LineWidth',2);
title('Average line of the variable 5')
Sudheer Bhimireddy
Sudheer Bhimireddy on 13 Aug 2020
Remove nexttitle, use hold on after the first plot(). Use legend to identify v5 and average line.
Your avg5 plot has x-axis that might not be the same as your previous plot(). It will only work if your time5 starts at 0 and has a step size of 1, which is not the case looking at your second attached figure. To make it generic, use the starting and ending values of averaging window in time5. That way both the plots will have same x-axis, which they should.
I see that you are plotting every point on x-axis, so the labels are clustered. After all plot() are done, you can specify the XTick points and their interval. See this.

Sign in to comment.

More Answers (1)

Shae Morgan
Shae Morgan on 13 Aug 2020
Wha about this:
t1 = datetime(2020,8,12,13,11,24); %start time
t2 = datetime(2020,8,12,13,18,36); %end time
time5 = t1:seconds:t2; %create a vector of time points from the start time to the end time
v5 = randn(size(time5)); %create random y-axis data
plot(time5,v5) %plot data
hold on; %hold the current figure so the first plot doesn't erase when you do the 2nd plot
avg5 = mean(v5); %the average value of v5
x2= [time5(1) time5(end)]; %x-values for the average line
plot(x2,[avg5 avg5], 'y-', 'LineWidth',2); %plot the avg line
title('value 5 by time plot') %title the plot
legend('Raw data','Average line') %add a legend to differentiate the two data sources
ax = gca; %get current axis
x_step_size=100; %step size between labels in seconds
ax.XAxis.TickValues = t1:seconds(x_step_size):t2; %adjust the tick values to the correct spacing

Categories

Find more on Discrete Data Plots 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!