MATLAB: Problem with indices (Subscript indices must either be real positive integers or logicals)

indicesscatter

I have a matrix with 3 indices (MxNxP) containing all doubles. The first two columns of any page give the x and y coordinates for that iteration (of each ray reflecting off mirrors). I use a square grid of starting points and then use "scatter" to plot the entry points on one chart and the exit points on another chart and everything works fine with this code:
figure
scatter(rays(:,1,1),rays(:,2,1),'*')
axis equal
title('All Ray Entry Points')
HOWEVER, if I add some amount of random +/- scatter to the original starting grid points with the following code by setting add_scatter=true:
if add_scatter == true
scatter = rand(M,2);
scatter(:,:) = scatter(:,:) - 0.5;
scatter(:,:) = scatter(:,:)*spacing*0.5;
rays(:,1:2) = rays(:,1:2) + scatter(:,:);
end
the scatter plot code returns the following error for every graph: "Subscript indices must either be real positive integers or logicals"
The matrix "rays" is still the same size and still contains only doubles. I can use the command window to see any location by putting any 3 index values in (e.g. rays(5,2,1)) but cannot generate any figures with the scatter plot function.
Any ideas???

Best Answer

You seem to be naming a variable scatter.
This will hide the function called scatter and mean that when you call
scatter(rays(:,1,1),rays(:,2,1),'*')
it will assume you are trying to index into the array called scatter rather than call the scatter function.
Never name a variable the same as a function! You can call e.g.
which scatter
if you want to check whether something is a function before using it as a variable in general, although obviously on this occasions you know scatter is a function since you were calling it!