Error in the loop

조회 수: 7 (최근 30일)
santosh
santosh 2011년 4월 6일
Hi all, I am trying to plot the y co-ordinates for different files with respect to one x value but when i execute the program it is showing the following error:
Attempted to access x(133); index out of bounds because numel(x)=132.Error in ==> new2 at 48 if x(j) == Z1
It is pointing the following lines of code:
for i=1:4 prompt1 = {'enter next input file name including extension'}; fname1 = inputdlg(prompt1); a = importdata(char(fname1), DELIMITER, HEADERLINES); new2()
for j = 1:200
if x(j) == Z1
Z2 = y(j)
break;
end
end
figure(1);
plot(Z1,Z2,'r*');
hold on;
end
Can anyone pls help me out,
regards
  댓글 수: 2
bym
bym 2011년 4월 6일
please post the code that defines y,Z1 and x before the loop.
essentially x(133) (and higher) don't exist
santosh
santosh 2011년 4월 6일
function [Z1,Z2,Z3,Z4]= new2(x,y)
% read .cor file
DELIMITER = '\t';
HEADERLINES = 74;
prompt1 = {'enter the input file name including extension'};
fname1 = inputdlg(prompt1);
% Import the file change the fileName
a = importdata(char(fname1), DELIMITER, HEADERLINES);
function [] = new2(~,~)
x = a.data(:,1);
y = a.data(:,2);
% clf;
figure(2);
plot(x,y);
hold on;
%%%%%%% joining the end points
figure(1);
plot(x([1 end]),y([1 end]));
end
new2()
Z1= median(x)
for i = 1:100
if x(i) == Z1
Z2 = y(i)
break;
end
end
figure(1);
plot(Z1,Z2,'r*');
hold on;
%%%%%%%%%% loop to take different files
for i=1:4
prompt1 = {'enter next input file name including extension'};
fname1 = inputdlg(prompt1);
a = importdata(char(fname1), DELIMITER, HEADERLINES);
new2()
for j = 1:200
if x(j) == Z1
Z2 = y(j)
break;
end
end
figure(1);
plot(Z1,Z2,'r*');
hold on;
end
end
this is the complete code and input files contains x whose index ends at 132

댓글을 달려면 로그인하십시오.

답변 (3개)

Matt Tearle
Matt Tearle 2011년 4월 6일
Well, the obvious answer is basically what the error message is saying: x only has 132 elements, but you're looping j up to 200. So when j gets to 133, the line if x(j) ... is going to fail.
What may be causing the problem is the ==. If x and Z1 are doubles, you may not get exact equality. If you were planning on that, that could be causing your indexing error. Try
if abs(x(j)-Z1) < tol
where tol is some small value.
Trying to interpret your code, it seems like you're trying to define Z2 to be the value of y at whichever index x is equal to Z1. If so, why not use logical indexing instead:
Z2 = y(x==Z1);
or, as discussed above,
Z2 = y(abs(x-Z1)<tol);
(This line replaces the whole for-loop, BTW)
  댓글 수: 2
santosh
santosh 2011년 4월 6일
Hi Matt, thanks alot for ur answer but still i m getting the same error if i use the line Z2 = y(x(i)==Z1);
inside the for loop
Matt Tearle
Matt Tearle 2011년 4월 6일
Again, this shouldn't be in a loop.
But, regardless, now you've posted the full code I can see the problem. I'll add another answer.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 4월 6일
Your function is named new2, and inside that function you define a subfunction that is also named new2. At least one of us is confused.
  댓글 수: 2
santosh
santosh 2011년 4월 6일
I changed it but stil i m getting the same error
Jan
Jan 2011년 4월 6일
Please update the posted code by editing your original question - and please use code formatting to improve the readability.

댓글을 달려면 로그인하십시오.


Matt Tearle
Matt Tearle 2011년 4월 6일
The problem is that Z1 is the median of x, and x has an even number of elements. In that case, the median is the average of the two middle values. If they're not the same, the median will not be any of the values in x. The real question is what y you want back in that case. The average of the two x values in the middle?
Z1 = median(x);
tmp = abs(x-Z1);
Z2 = mean(y(tmp == min(tmp)));
This will work for any number of elements of x.
  댓글 수: 1
Matt Tearle
Matt Tearle 2011년 4월 6일
Just to clarify, these three lines replace
Z1= median(x)
for i = 1:100
if x(i) == Z1
Z2 = y(i)
break;
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by