ERROR // Array indices must be positive integers or local values

Hello! I have been working on this script for a little bit, and cant seem to figure out why I am getting this error:
" Array indices must be positive integers or logical values.
Error in Q1 (line 33)
set(h,'Color',colorData(rand1)); "
The program is meant to animate a point traveling along a path over over time, changing marker and color randomly with each point. It also reports the runtime of the overall script.
The error changes line every time the script runs between the different set functions. Sometimes it runs fine with no problem. After attempting to debug with disp, I have not seen a value that aligns with the error. Am I missing something?
%%%%%%SCRIPT%%%%%%%
tic
file = fopen('DATA.xlsx');
data = xlsread('DATA','sheet1','A1:B20');
x = data(:,1);
y = data(:,2);
colorData = ["red","green","blue","cyan","magenta","yellow","black","white"];
shapeData = ["o","+","*",".","x","_","|","square","diamond","^","v",">","<","pentagram","hexagram"];
rand1 = 0;
rand2 = 0;
figure(1);
h = plot(0,0);
xlim([0 21]);
ylim([0 6])
for ii = 1:20
rand1 = round(rand*8);
rand2 = round(rand*15);
%x(ii),y(ii),'Color',colorData(rand1),'Marker',shapeData(rand2)
set(h,'Xdata',x(ii));
set(h,'Ydata',y(ii));
disp(rand1)
set(h,'Color',colorData(rand1));
set(h,'Marker',shapeData(rand2));
%I DONT UNDERSTAND WHY IT WORKS SOMETIMES AND NOT OTHERS :(
%drawnow;flushdata;
pause(0.2);
end
t = toc;
fprintf(append('The total program runtime was: ',num2str(t)));

 採用された回答

Voss
Voss 2022 年 12 月 15 日

0 投票

The error happens because sometimes the random value returned from rand is small enough that rand1 or rand2 is 0.
Try replacing this
rand1 = round(rand*8);
rand2 = round(rand*15);
with this:
rand1 = randi(8);
rand2 = randi(15);

3 件のコメント

Voss
Voss 2022 年 12 月 15 日
Better yet, store the number of colors and the number of markers in variables and use those, rather than hard-coding 8 and 15. That way if colorData or shapeData changes size in the future (e.g., you decide to allow only 9 markers instead of 15), you don't have to remember to change the hard-coded value further down in the code.
tic
file = fopen('DATA.xlsx');
data = xlsread('DATA','sheet1','A1:B20');
x = data(:,1);
y = data(:,2);
colorData = ["red","green","blue","cyan","magenta","yellow","black","white"];
shapeData = ["o","+","*",".","x","_","|","square","diamond","^","v",">","<","pentagram","hexagram"];
% rand1 = 0;
% rand2 = 0;
figure(1);
h = plot(0,0);
xlim([0 21]);
ylim([0 6])
n_colors = numel(colorData);
n_shapes = numel(shapeData);
for ii = 1:numel(x)
rand1 = randi(n_colors);
rand2 = randi(n_shapes);
%x(ii),y(ii),'Color',colorData(rand1),'Marker',shapeData(rand2)
set(h,'Xdata',x(ii));
set(h,'Ydata',y(ii));
disp(rand1)
set(h,'Color',colorData(rand1));
set(h,'Marker',shapeData(rand2));
%I DONT UNDERSTAND WHY IT WORKS SOMETIMES AND NOT OTHERS :(
%drawnow;flushdata;
pause(0.2);
end
t = toc;
fprintf(append('The total program runtime was: ',num2str(t)));
Ryan Coder
Ryan Coder 2022 年 12 月 15 日
Thanks!!
Voss
Voss 2022 年 12 月 15 日
You're welcome!

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

その他の回答 (2 件)

Bora Eryilmaz
Bora Eryilmaz 2022 年 12 月 15 日
編集済み: Bora Eryilmaz 2022 年 12 月 15 日
In MATLAB, array indexing is 1-based, not 0-based. The first element of colorData, etc. would be colorData(1). Occasionally, rand command would produce a small enough value such that
rand1 = round(rand*8)
rand1 = 8
would be 0. So, instead of round(), use ceil(), which would guarantee value at or above 1:
rand1 = ceil(rand*8)
rand1 = 6
Better yet, use randi() to produce integer random numbers: https://www.mathworks.com/help/matlab/ref/randi.html

1 件のコメント

Ryan Coder
Ryan Coder 2022 年 12 月 15 日
Thanks for the help!! Totally missed that.

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

millercommamatt
millercommamatt 2022 年 12 月 15 日

0 投票

It's because you can round to zero which is invalid as an index. Use ceil instead.

カテゴリ

ヘルプ センター および File ExchangeLine Plots についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 12 月 15 日

コメント済み:

2022 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by