Dywabo
Dywabo

Reputation: 49

Errors creating plots/averaging

Could someone tell me why one of my graphics (blue) doesn't show on the plot and the other (black) beggins too early (to avoid this I tried to put those values as NaN and it worked with the red graphic)? I think it's because I'm not writing the function for the black correctly. I want to make the average for 21 years from the data I'm given, so the first 10 correspondent x can't have values on the graphic. A

        C = textscan(fid,'%f %f %f','headerlines',32,'commentstyle','--');

        x = C{1}; 
        y1 = C{2};
        y2 = C{3};
        z = C{2};

        sum_21mt = 0;
        for i = 11:153
            sum_21mt = soma_21mt + z(21);
            med_21mt = soma_21mt/21;
        end

        y1(y1==-99.99) = NaN;
        y2(y2==-99.99) = NaN;
        z1 = z(1:10);
        z2 = z(154:length(z));
        z1 = NaN;
        z2 = NaN;

        plot(x, y1, 'b-', x, y2, 'r-', x, z, 'k-');

Upvotes: 0

Views: 105

Answers (2)

Aero Engy
Aero Engy

Reputation: 3608

z and y1 are identical in your code sample. So the blue line and black line are identical. Therefore the blue line is hidden under the black one since the black one was created last. With Tobold's fix to NaN-ing out some z values, I suspect you see a blue/black/blue line.

As mentioned your for loop doesn't really do anything. However, if you are trying to do a simple running averaging filter on z try the following. Matlab filter function

windowSize = 21;
z = filter(ones(1,windowSize)/windowSize,1,y1);

I prefer Matlab's filtfilt() function since it is a forward and reverse filter but that requires the Signal Processing toolbox.

Edit: Also as a note since that is a non-initialized filter the first windowsize-1 values will be skewed. There are ways to initialize your filter but that is going beyond your question.

Your for loop still isn't doing anything. You need to index into y1 with your i/j. I think you are trying to do a centered average where a point in z is equal to the average of a value in y1 +/- 10 samples. Here is some code that will do just that.

% Define half window.  True window size is 2*halfWindow+1
halfWindow = 10; 

% Init z to zeros.  
% There will be halfWindow worth of zeros at the beginning and end of z after the loop.
z = zeros(size(y1)); 

%Loop starting at 11 (if halfWindow = 10) and ending 10 from the end.
for i = (1+halfWindow):(length(y1)-halfWindow)
    z(i) = mean(y1(i-halfWindow:i+halfWindow)); %Take mean of current point +/- 10 samples
end

I would probably still use the filter function instead of the above code and learn how to initialize said filter. But at least that should show you how to index into arrays.

Upvotes: 1

Tobias
Tobias

Reputation: 4074

You don't set anything to NaN in z. I guess what you want to do is

    z(1:10) = NaN;
    z(154:length(z)) = NaN;

instead of

    z1 = z(1:10);
    z2 = z(154:length(z));
    z1 = NaN;
    z2 = NaN;

Also your for loop doesn't seem to accomplish what I think you want it to.

Upvotes: 1

Related Questions