Index exceeds the number of array elements (20)
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi all, I recently tried to edit some code I had that analyzes images. I wanted it to be able to run through an entire folder of images instead of having to re-run the program for each one. When I first open Matlab, it works for the first iteration of the loop but then crashes, and then it won't even go through the first iteration on subsequent runs. I'm not quite sure whats going on. The error message points to line 26: figure, imshow(grayimg,'InitialMagnification','fit'),title(fileName), hold on
inFolder = uigetdir;
imgFiles = dir(fullfile(inFolder,'*.jpg'))
nfiles = length(imgFiles);
for j = 1 : nfiles
fileName = fullfile(inFolder, imgFiles(j).name);
imgcolor=imread(fileName);
grayimg=rgb2gray(imgcolor);
BW=edge(grayimg,'canny');
%Mathematically perform hough transform
%hough transform angles for horizontal area
[H,theta,rho]=hough(BW,'Theta',[-90:0.1:-85,85:0.1:89.5]); %[-90:0.5:-60,60:0.5:89]
peak=houghpeaks(H,1);
x=theta(peak(:,2));
y=rho(peak(:,1));
lines = houghlines(BW,theta,rho,peak,'FillGap',10,'MinLength',10);
figure, imshow(grayimg,'InitialMagnification','fit'),title(fileName), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','blue');
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
댓글 수: 1
Adam
2019년 5월 15일
What does
which title
show if you put in a breakpoint on that line (or better use the stop on errors to have it stop there when the error actually occurs)?
You often get these kind of errors when you have accidentally over-written a function name with a variable of the same name so what you think is a function call Matlab is interpreting as you trying to index into a variable.
Since there is no indexing on the line you say gives the error this would lead me to think that you have a variable called title, that it is attempting to index into with fileName and it is failing because your title array only has 20 elements and fileName evaluates to goodness knows what number larger than 20 when interpreted as a number.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!