Clear Filters
Clear Filters

How can I fix this code?

8 views (last 30 days)
Ridley on 25 May 2024 at 13:27
Edited: Image Analyst on 25 May 2024 at 16:32
[xb, yb] = getpts; %click on the bases you would like to add
new_coords = [yb xb]
imgcoordsRC = [imgcoordsRC(:,1:2); new_coords];
idx2 = sub2ind([1520 2704],floor(imgcoordsRC(:,1)),floor(imgcoordsRC(:,2)));
imgcoords = zeros([1520 2704]);
imgcoords(idx2) = 1;
Index exceeds the number of array elements. Index must not exceed 1.
  1 Comment
Torsten on 25 May 2024 at 14:08
My guess is that "imgcoordsRC" has only one column, but you try to reference "imgcoordsRC(:,2)" in your code.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 25 May 2024 at 16:29
Edited: Image Analyst on 25 May 2024 at 16:32
I don't see how imshow would throw that error. So now we're left wondering exactly which line of code threw the error. Because you didn't read the posting guidelines and didn't give us the full error message, we don't know and just have to guess. I would not like to guess at possible reasons for all of those lines, why they might fail. So give us the full error message, ALL the red text, including the actual line number and line of code that threw the error. All you gave was the actual error message but not the line number and line of code. Also put this in front of that code and tell us what you see in the command window:
whos imgcoordsRC
whos coordsImage
and then after you make new_coords, put this:
whos new_coords
And after you make idx2, show us what it is:
whos idx2
And why are you showing coordsImage after that code when apparently none of that code made any change to the image?
Also you probably should not hard code in 1520 2704], you should probably get the size from somewhere (Is it the image???) and use it as a variable.
If you never instantiate imgcoordsRC then you can't append to it so you can instantiate it like I show below.
So the final code should be
whos imgcoordsRC
whos coordsImage
[xb, yb] = getpts; % Click on the bases you would like to add
new_coords = [yb, xb] % [Row, Column]
if isempty(imgcoordsRC) % Check if it's not been assigned yet.
% Create initial matrix since it doesn't exist yet.
imgcoordsRC = new_coords;
% Vertically concatenate onto bottom of existing matrix.
imgcoordsRC = [imgcoordsRC(:,1:2); new_coords];
whos new_coords
[rows, columns, numberOfColorChannels] = size(coordsImage)
idx2 = sub2ind([rows, columns], floor(imgcoordsRC(:,1)), floor(imgcoordsRC(:,2)));
whos idx2
imgcoords = zeros([rows, columns]);
imgcoords(idx2) = 1;
imshow(coordsImage, []);




Community Treasure Hunt

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

Start Hunting!