MATLAB: Set Maximum Limit of Array, but keep Gradients the Same.

arraylimitplot

I have an array Batt_C that contains 527040 values (data set of Watts for each minute throughout a year), ranging from -2.8373e+05 : 2.2426e+06.
I want to set the maximum limit in the array which I can do, at size = 1.3222e+06 using:
Batt_C(Batt_C>size) = Batt_C;
If these are plotted I want the gradients from the original Batt_C to transfer to the limited Batt_C array. I.e. if the values go down on the original, then I want the limited to also show the same downward shape. And then rise again accordingly to the limit.

Best Answer

First, do not use size as a variable name as it will prevent you from using the extremely important size function.
Secondly, the line
Batt_C(Batt_C>size) = Batt_C;
will only succeed if no element of Batt_C is greater than size. Otherwise you'll get a dimension mismatch error. I assume the right hand side is supposed to be size.
Anyway, to answer your question, you'll have to identify each continuous run of values above your limit (henceforth called limit not size). Once you've down that subtract from each run the difference from the first element of the run and the limit.
There are several files on the file exchange that can identify runs, it's fairly easy to implement anyway:
abovelimit = Batt_C(:) > limit;
%get start and ends of runs:
runstarts = find(diff([0; abovelimit]) == 1);
runends = find(diff([abovelimit; 0]) == -1);
%adjust each run:
for run = 1:numel(runstarts)
Batt_C(runstarts(run):runends(run)) = Batt_C(runstarts(run):runends(run)) - ...
(Batt_C(runstarts(run)) - limit);
end