How to subtract previous array element from the current one ?
조회 수: 18 (최근 30일)
이전 댓글 표시
Mustafa Alper Cetintas
2021년 6월 25일
편집: Mustafa Alper Cetintas
2021년 6월 25일
I have datastream as lines from the comport. All values are being parsed and I can choose the interested data. In this case, I want to subtract previous Easting value from the current one and calculate the distance between these two values each second when new string received. Could not create a 'for' loop for this operation;
s=serialport('COM14',115200);
configureTerminator(s,"CR/LF");
t = 0;
a = 0;
while true
t = t + 1;
line=readline(s);
ld=split(line,',');
Date=(ld{1});
UTC=(ld{2});
LAT=(str2double(ld{3}));
LONG=(str2double(ld{4}));
ALT=(str2double(ld{5}));
[Easting,Northing,UTMZone]=deg2utm(LAT,LONG);
HPOS=[Easting Northing];
SoG=(ld{6});
Depth=(ld{7});
CL=(ld{8});
AX=(str2double(ld{9}));
AY=(str2double(ld{10}));
AZ=(str2double(ld{11}));
GX=(str2double(ld{12}));
GY=(str2double(ld{13}));
GZ=(str2double(ld{14}));
MX=(str2double(ld{15}));
MY=(str2double(ld{16}));
MZ=(str2double(ld{17}));
Temp=(ld{18});
Bat=(ld{19});
ii=1:length(Date);
jj=2:length(Date);
test=zeros(length(HPOS),1);
for ix=1:length(Date)
test(ix)=HPOS(jj)-HPOS(ii);
end
end
when I run this code, I get the error message saying;
Index exceeds the number of array elements (2).
Error in Untitled5 (line 46)
test(ix)=HPOS(jj)-HPOS(ii);
Kind Regards,
Alper
댓글 수: 0
채택된 답변
Scott MacKenzie
2021년 6월 25일
편집: Scott MacKenzie
2021년 6월 25일
If you want to
subtract previous Easting value from the current one
see below. The value you want is EastingDiff.
EastingSave = 0;
while true
...
[Easting,Northing,UTMZone]=deg2utm(LAT,LONG);
EastingDiff = Easting - EastingSave;
EastingSave = Easting;
...
end
To avoid the error, get rid of the six lines beginning with ii = . You don't need them.
댓글 수: 1
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!