Why am I getting this error? "Array indices must be positive integers or logical values".
1 次查看(过去 30 天)
显示 更早的评论
I am getting the error "Array indices must be positive integers or logical values" when I run the code below. It says the error is in the line that I have bolded.
L_SEC0 = 239;
P0 = 1060;
stiff = 440;
L_CC0 = 122;
t_rise = 120;
t_fall = 60;
C0 = 6.25;
a = 400;
b = 150;
L_total = L_SEC0 + L_CC0;
duration = 0.2;
fs = 10,000;
t_step = 1/fs;
time = 0:t_step:duration;
points = duration*fs;
P_twitch_short = zeros(points,1);
P_twitch_long = zeros(points,1);
P_tetanus = zeros(points,1);
SA = zeros(points,1);
L_SEC = L_SEC0;
L_CC = L_CC0;
for i = 1:length(time)
RLS = (L_SEC-L_SEC0)/L_SEC0;
P_twitch_short(i) = P0(0.0258*(exp((stiff*RLS)))-0.0258);
0 个评论
采纳的回答
Voss
2022-11-27
On that line, everything inside the outermost parentheses on the right-hand side is an index into P0:
P_twitch_short(i) = P0(0.0258*(exp((stiff*RLS)))-0.0258);
% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index into P0
If that expression, 0.0258*(exp((stiff*RLS)))-0.0258, is not a positive integer, you get the error you got.
Maybe you meant to multiply that by P0 instead of indexing into P0?
P_twitch_short(i) = P0*(0.0258*(exp((stiff*RLS)))-0.0258);
% ^ multiplication
更多回答(1 个)
John D'Errico
2022-11-27
编辑:John D'Errico
2022-11-27
Is P0 a function? (NO. It is a scalar variable.)
You have this:
P_twitch_short(i) = P0(0.0258*(exp((stiff*RLS)))-0.0258);
Did you intend to multiply by P0? Does MATLAB allow implicit multiplication, where you don't need to use an operator?
What will happen then you write
P0(stuff)
instead of
P0*(stuff)
Any guesses? The point is, MATLAB thinks either you must be calling a function named P0 at that point, OR you are trying to index the variable P0. But since it knows that P0 is a variable, it then tries to use what is inside the parens as an index for that variable. And so when that fails, MATLAB gets upset, and returns an error.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!