# Why am I getting this error? "Array indices must be positive integers or logical values".

6 ビュー (過去 30 日間)
Josh Riesenberg 2022 年 11 月 27 日

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);

サインインしてコメントする。

### 採用された回答

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
##### 2 件のコメント表示非表示 1 件の古いコメント
Voss 2022 年 11 月 27 日

You're welcome!

サインインしてコメントする。

### その他の回答 (1 件)

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)
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.
##### 1 件のコメント表示非表示 なし
Josh Riesenberg 2022 年 11 月 27 日
Thank you!

サインインしてコメントする。

### カテゴリ

Find more on Matrices and Arrays in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!