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