Plotting the average line of a graph of a column imported from an excel
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ramo Rafsel
am 7 Aug. 2020
Bearbeitet: Ramo Rafsel
am 19 Aug. 2020
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;
0 Kommentare
Akzeptierte Antwort
Sudheer Bhimireddy
am 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 Kommentare
Sudheer Bhimireddy
am 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.
Weitere Antworten (1)
Shae Morgan
am 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
1 Kommentar
Siehe auch
Kategorien
Mehr zu Discrete Data Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!