invalid index in find function

2 次查看(过去 30 天)
Wrote this function to detect a line on a .png file of a graph, read the coordinates and use them to recalculate the graph data:
function TC = fileReadTest()
%set data at graph limits
Tfmax = 2000;
tmax = 2000;
[pixels]=imread("temp597.jpg"); % read rgb values for each pixel on graph
imshow(pixels)
[x_axes,y_axes]=ginput(2);% select axes limits (y limit then x limit)
data(2,x_axes(2)-x_axes(1)) = zeros; %define the length of the data list
for column=x_axes(1):x_axes(2) %loop for each column inside the graph
%find the first red(ish) pixel in the column
[row, col]=find(pixels(:,column,1)>200 && pixels(:,column,2)<50,1,'first');
if (col == column) %append data list if red pixel is found in the column
data(:,col) = [row;col]-[x_axes(1);y_axes(2)];
end
end
% convert data coordinate to usable value
Tf=[Tfmax/(y_axes(1)-y_axes(2))*data(1,:);tmax/(x_axes(2)-x_axes(1))*data(2,:)];
TC=(Tf(1,:)-32)*5/9;
got the following error:
Index in position 2 is invalid. Array indices must be positive
integers or logical values.
Error in fileReadTest (line 14)
[row, col]=find(pixels(:,column,1)>200 && pixels(:,column,2)<50,1,'first');

采纳的回答

Steven Lord
Steven Lord 2023-2-23
There's no guarantee that the elements of x_axes (the points that you selected using ginput) are integer values. Therefore there's no guarantee that the values assigned to your loop variable column are integer values. MATLAB can't index into the 125.3827th column (as an example) of your pixels array.
Perhaps it would be sufficient if you were to round those coordinates before using them in your loop.
  3 个评论
Steven Lord
Steven Lord 2023-2-25
You can't use the short-circuiting && or || operators when either of the inputs you're operating on are non-scalars. [If you could, and the first input contained a mix of true and false values, should the second input only be evalated for those elements where their values could change what the logical operators return? That could get very messy very quickly.]
Use the non-short circuiting & or | operators instead.
x = 1:10;
y = 10:-1:1;
z = (x > 3) & (y > 4)
z = 1×10 logical array
0 0 0 1 1 1 0 0 0 0
[x; y; z]
ans = 3×10
1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 0 0 0 1 1 1 0 0 0 0

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by