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

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;

 Réponse acceptée

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 commentaires

Ramo Rafsel
Ramo Rafsel le 10 Août 2020
Modifié(e) : Ramo Rafsel le 10 Août 2020
Yours worked better than mine somehow. I have been trying the loop that I wrote above in the code, so that I don't have to check how long or how many rows the column has. But I think i will find that out :).
Thanks a lot for your help :)
After solving the average line problem I encountered another hurdle, when I added a second column with the time(HH:MM:SS) when the values where written in the file, since I plotted the time and the value column on the x axis and y axis respectively (see the code below). I cannot plot the average line of the value column in the same figure since the x axis and y axis are defined already by the 2 columns. Any Ideas will be much appreciated :)
avgline= sum(value_column)/size(value_column),1); %the value_column and time column are already defined from the workspace)
nexttile
plot(time_column,value_column)
hold on
plot(avgline,'-r','LineWidth',2);
title('T°C')
hold off
%The Error that I get:
%Values plotted against x-axis must be categorical values. To create categorical values, use the categorical function.
First, your avgline code seems strange, may be a typo while pasting here. Check the dimensions of avgline. It should read:
avgline= sum(value_column)/size(value_column,1);
Second, to plot the avgline on the same axis, try explicitly specifying the x-axis. I mean, use
plot([min(time_column) max(time_column)],avgline,'-r','LineWidth',2);
@Sudheer Bhimireddy First of all, thanks a lot again for your help.
Yes it was a typo, instead of writing size(value_column,1) I added the ")".
I just used the min max of the time_column but it shows me that there is an error, with no specifications written.
If you can, please attach your code.
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')
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.

Connectez-vous pour commenter.

Plus de réponses (1)

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

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by