Index exceeds the number of array elements (0)

1 次查看(过去 30 天)
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 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by