Index exceeds the number of array elements (0)

1 view (last 30 days)
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 Comments
Mario Malic
Mario Malic on 7 Dec 2020
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 on 7 Dec 2020
Edited: Avi Michaely on 7 Dec 2020
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! :)

Sign in to comment.

Accepted Answer

Mario Malic
Mario Malic on 7 Dec 2020
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!