필터 지우기
필터 지우기

why do I have this error in index

조회 수: 2 (최근 30일)
lakom Mariem
lakom Mariem 2017년 10월 2일
편집: OCDER 2017년 10월 2일
Hello Can anyone help me please I have this error in matlab in plotting
Error in prob (line 13)
Vc1(i)= f1(indx(i));
So can anyone help me
you can see my code
clear all;
indx=[0.0:4.99:50.0];
f1=@(x) Ceoscj(4,p);
f2=@(x) Cesow(4,p);
f3=@(x) Oscj(4,p);
f4=@(x) Swb(4,p);
f5=@(x) Ssj(4,p);
for i=1:size(indx,2)
Vc1(i)= f1(indx(i));
Vc2(i)= f2(indx(i));
Vc3(i)= f3(indx(i));
Vc4(i)= f4(indx(i));
Vc5(i)= f5(indx(i));
end
plot(indx,Vc1,'-o');
hold on;
plot(indx,Vc2,'-*');
hold on;
plot(indx,Vc3,'-s');
hold on;
plot(indx,Vc4,'-d');
hold on;
plot(indx,Vc5,'-m');
hold on;
leg=legend('Cesosj','Cesow','Oscj','Ssj');
set(leg,'location','best')
set(gca,'XTick',(0:5:50))
set(gca,'YTick',(0:10^-1:10^0))
xlabel('P [dB]');
ylabel('Secrecy Outage Probability');
thanks in advance
  댓글 수: 3
lakom Mariem
lakom Mariem 2017년 10월 2일
Error in @(x)Ceoscj(4,p)
Error in prob (line 13) Vc1(i)= f1(indx(i));
Jan
Jan 2017년 10월 2일
@lakom Mariem: Are you really sure that this is the complete message? It tells only, where the error occurs, but not, what the problem is. This is really unusual for error messages in Matlab.

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

답변 (2개)

Star Strider
Star Strider 2017년 10월 2일
You didn’t say what the error is, although I am guessing that it is ‘Undefined function or variable 'p'.’.
Note that here:
f1=@(x) Ceoscj(4,p);
the argument to ‘f1’ is ‘x’, and does not refer to ‘p’ (that is apparently undefined in your code).
  댓글 수: 1
Jan
Jan 2017년 10월 2일
편집: Jan 2017년 10월 2일
@lakom Mariem: The p must be undefined after the clear all.

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


OCDER
OCDER 2017년 10월 2일
편집: OCDER 2017년 10월 2일
You are accessing a matrix using a non-integer index right here, which is not allowed. Index must be an integer > 0. Another issue is that your function handle uses 'x' as a variable, but you have 'p' instead. See comments below:
indx=[0.0:4.99:50.0]; %your index is not an integer > 0. What is the 0th element of a vector?
f1=@(x) Ceoscj(4,p); %variable is x, but you use p. To fix, use: f1 = @(x) Ceoscj(4,x)
for i=1:size(indx,2) %use numel. for i = 1:numel(indx)
Vc1(i)= f1(indx(i)); %here, f1(indx(1)) = f1(0). What is the 0th element of f1?
%Also, you do not preallocate Vc1, meaning this will be slow due to
% matrix creation, deletion, and copying every time the size changes in a loop.
end
plot(indx,Vc1,'-o'); %indx is used only as a x value, so maybe rename indx to x.
% Your "i" in the for loop counter is the real "index" variable.
Using all these loops and function handles is quite hard to maintain in the long run. I would instead try to make your functions like Ceoscj take in a vector input x and return a vector output. That way, you could do something like this:
x =[0.0:4.99:50.0];
Vc1 = Ceoscj(4, x);
plot(x, yVc1)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by