Array indices must be positive integers or logical values.

2 ビュー (過去 30 日間)
Tzu-Yun Chang
Tzu-Yun Chang 2021 年 1 月 28 日
コメント済み: Walter Roberson 2021 年 1 月 28 日
I am trying to plot a v-t graph which pwm(t, T, d) wil be set to 1 if kT ≤ t < (k + d)T and to 0 if (k + d)T ≤ t < (k + 1)T. The below is my script:
T = 2*pi;
d = 0.5;
syms v;
for t = [0:1:10]
v(t) = pwmmode (t,T,d);
end
plot (t,v);
function y = pwmmode (t,T,d)
k = 1;
if ((k*T <= t) & (t <= (k+d)*T)) y =1;
elseif (((k+d)*T <= t) & (t<(k+1)*T)) y=0;
else y = 2;
end
end
When I run my script above, I receive errors in the window. Below are the errors displayed on my window:
  1 件のコメント
Stephen23
Stephen23 2021 年 1 月 28 日
for t = [0:1:10]
v(t) = ...
end
Zero is not a valid index.

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

回答 (1 件)

KSSV
KSSV 2021 年 1 月 28 日
In MATLAB arrays indices cannot be negative/ zeros. Your code:
for t = [0:1:10]
v(t) = pwmmode (t,T,d);
end
Should be like:
t = 0:1:10 ;
v = zeros(size(t)) ; % intialize accordingly
for i = 1:length(t)
v(i) = pwmmode (t(i),T,d);
end
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 1 月 28 日
Or in this particular case you could abbreviate to
for t = [0:1:10]
v(t+1) = pwmmode (t,T,d);
end
However, KSSV's suggstion of creating the list of values ahead of time and looping through indices is a more general approach that you should learn how to use. For example if you were to use
for t = [0:1/3:10]
v(3*t+1) = pwmmode (t,T,d);
end
then you would fail because the t values would not be exact multiples of 1/3

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by