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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by