How to add the noise covariance matrix of my measurements to tmy 1D kalman filter? So I am new to kalman filters. This is my first experience with it.

I have some measurements but sometimes due to external factors those measurements become totally wrong (hence the peak when you run my matlab code). If I understand the principles of kalman filtering correctly, I can compensate this incorrect measurements by setting the measurement covariance matrix R as a variable, where R represents the accuracy of the measurement devide. But so far I am not sure about where I should add or how I should include this measurement covariance matrix in my measurements.

Could anybody help me out?

I have tried as follows: https://pastebin.com/KQEx5T10 But am totally not sure about my results.To me it looks like both of the outputs of my kalman filter are too far away from the actual measurements once the peak happened... I would have expected the output curves to be between the "toothshaped" curve and not above it.
If you are so new to Kalman filtering, the best first suggestion should be the most readable book on the topic: Fundamentals of Kalman Filtering, from Zarchan. 

In the mean time, the measurement noise covariance matrix $R$ is adjusted according to the a priori known (either empirically or theoretically) characteristics of the measurement device (the sensors). When these characteristics change during the operation time of the filter, then you would expectedly reflect that in an update of the measurement noise matrix $R$ according to the new settings. 

You will include this measurement noise covariance matrix into the Kalman update equations. Which previously using fixed $R$ will now be using variable $R$ matrix, as a matter of fact.