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

excelfunctionimporting excel datameanplotplottingsum

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;

Best Answer

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.