필터 지우기
필터 지우기

Formatting data using an if function

조회 수: 1 (최근 30일)
Benedikt Wessel
Benedikt Wessel 2018년 9월 21일
댓글: Benedikt Wessel 2018년 9월 22일
Hello everybody, how can I adjust the data, so that the purple curve will always be connected? The next purple section should start at the end of the section before.... I've no clue how to do that. I really need help, because of this thing I'm not able to finish my bachelor thesis. Thanks guys.
(Ignore the legend)
C=cumsum(diffE,'omitnan'); % needed Capacity
Cbmax= 1000; % in MWH, max. capacity of the battery storage
Cbmin= 100; % in MWh, deep charge border
imax= C>=Cbmax-Cbmin; % find where C is larger than the Capacity
imin= C<=0; % find where C is =0
C(imax) = Cbmax-Cbmin; % if C is larger than Cbmax: C=Cbmax
C(imin) = Cbmin-Cbmin; % if C is smaller than Cbmin: C=Cbmin
Cres=cumsum(diffE,'omitnan');
for k=1:length(Cres)-1
if Cres(k)>(Cbmax-Cbmin) && Cres(k)>Cres(k+1)
C(k+1)=C(k)-Cres(k)+Cres(k+1);
end
if Cres(k)>(Cbmax-Cbmin) && C(k)<(Cbmax-Cbmin)
C(k+1)=C(k)-Cres(k)+Cres(k+1);
end
end

채택된 답변

Stephen23
Stephen23 2018년 9월 21일
편집: Stephen23 2018년 9월 22일
"how can I adjust the data, so that the purple curve will always be connected?"
  • loop over the length of the data vector.
  • detect any discontinuities (e.g. difference/offset, or some other metric).
  • shift the remaining data by the detected offset (use indexing).
Something like this:
for k = 2:numel(vec)
d = diff(vec(k-1:k))
if abs(d)>tol % pick a tolerance, or some metric
vec(k:end) = vec(k:end)-d;
end
end
I just tried this on a fake data vector:
tol = 2;
vec = [0,1,2,3,4,10,11,12,13];
giving:
>> vec
vec =
0 1 2 3 4 4 5 6 7
You could fine-tune the algorithm so that it includes an offset of the same sign as d, but with magnitude tol. Also it would pay to do some reading into detecting discontinuities:
  댓글 수: 3
Stephen23
Stephen23 2018년 9월 22일
편집: Stephen23 2018년 9월 22일
"But unfortunately the diff functions is always in an endless loop"
Check if you have any other functions named diff:
which diff -all
And if so, rename them. Also ensure that you do not have any variables name diff. Note that diff is not strictly required, you could do this:
d = vec(k)-vec(k-1)
Benedikt Wessel
Benedikt Wessel 2018년 9월 22일
Oh nice, now it's working. I although tried to use indexing to find the gaps and to adjust them, but my code is a mess I think. Sorry to bother you, but can you find my mistakes? I also added the needed data if it's helpful. Here's my code:
C=cumsum(diffE,'omitnan'); % needed Capacity, will be adjusted by Cbmax anc Cbmin
Cres=C;
Cbmax= 1000; % in MWH, max. capacity of the battery storage
Cbmin= 100; % in MWh, deep charge border
imax= C>=Cbmax-Cbmin; % find where C is larger than the Capacity
imin= C<=0; % find where C is =0
C(imax) = Cbmax-Cbmin; % if C is larger than Cbmax: C=Cbmax
C(imin) = Cbmin-Cbmin; % if C is smaller than Cbmin: C=Cbmin
Cres=cumsum(diffE,'omitnan');
change=zeros(length(diffE),1);
for k=1:length(Cres)-1
if Cres(k)>(Cbmax-Cbmin) && Cres(k)>Cres(k+1)
C(k+1)=C(k)-Cres(k)+Cres(k+1);
end
if Cres(k)>(Cbmax-Cbmin) && C(k)<(Cbmax-Cbmin)
C(k+1)=C(k)-Cres(k)+Cres(k+1);
end
change(k)= Cres(k)>(Cbmax-Cbmin) && Cres(k+1)<(Cbmax-Cbmin);
end
cha=find(change==1); % find the positions of the gaps between the sections
for k = 1:length(C)-1
for n=cha
if C(k)==C(n) % find the data with the "crack"
C(k:n+1) = C(k:n+1)-C(n)-C(n-1); % subtract every value in the actual section with the difference between the sections
end
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 9월 21일
  댓글 수: 1
Benedikt Wessel
Benedikt Wessel 2018년 9월 21일
Thanks. But I don't need to interpolate the values. I need all values to be lower to fit the section before.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by