Index exceeds the number of array elements (0)

조회 수: 1 (최근 30일)
Avi Michaely
Avi Michaely 2020년 12월 6일
답변: Mario Malic 2020년 12월 7일
Hello. I'm having this error in my code and I can't get why. I am trying to make it so if the user doesn't move the analog stick, the drone is hovering in its' place
function [] = calcThrust(app)
if (app.remoteState.Gamepad.LeftThumbY > 5000 || app.remoteState.Gamepad.LeftThumbY < -5000)
app.droneThrust = (app.maxDroneThrust - app.hoverThrust) * double(app.remoteState.Gamepad.LeftThumbY) / 32768 + app.hoverThrust;
if app.droneThrust < -app.maxDroneThrust % Descending
app.droneThrust = -app.maxDroneThrust;
elseif app.droneThrust > app.maxDroneThrust
app.droneThrust = app.maxDroneThrust;
end
if (app.droneThrust < app.hoverThrust) & (app.dronePos(3) <= app.minHoverHeight)
app.droneThrust = app.hoverThrust;
% disp(app.droneThrust + " " + app.dronePos(3));
end
else
app.droneThrust = app.hoverThrust; % <- The error is in this line
end
end
The error is in line "app.droneThrust = app.hoverThrust;", if I comment this line everything works. I don't understand why, because both of these variables are numbers and not indexed..
  댓글 수: 4
Mario Malic
Mario Malic 2020년 12월 7일
Both of these app.droneWeightKG, app.earthGravity are also correctly defined?
If one of the variables is empty, then resulting variable will be empty as well, which will throw the error in your title. If you defined them correctly, then you should check debugging. Set a breakpoint at that line and check the values in the workspace.
Avi Michaely
Avi Michaely 2020년 12월 7일
편집: Avi Michaely 2020년 12월 7일
Yep, they are both constants:
droneWeightKG = 0.905; % drone weight.. in kg
earthGravity = 9.819; % [m /s^2]
I'll try debugging and see what happens
EDIT: Thanks! When debugging I saw hoverThrust is 0x0 double because I called the timers before defining hoverThrust in the startup function, I switched the order and now it works! :)

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

채택된 답변

Mario Malic
Mario Malic 2020년 12월 7일
If earthGravity and hoverThrust are not properties of the app, then that's why it doesn't work.
properties (Access = public)
droneWeightKG
earthGravity
hoverThrust
end
function startupFcn(app)
app.droneWeightKG = 0.905; % drone weight.. in kg
app.earthGravity = 9.819; % [m /s^2]
end
You can define them as varuables in the calcThrust function, but then you'd have to change some of the code, above might be more appropriate solution, depending on the complexity of the app.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by