why I got error in MATLAB 2012a " Undefined function or variable 'p'. The first assignment to a local variable determines its class."when i run my code?

1 次查看(过去 30 天)
//This is my code
function p = fcn(GLE,s)
coder.extrinsic('pause');
p=p+1;
while (p<=4)
n=GLE;
end;
while ((p==1) && (n>=5))
g1=1;
r1=0;
y1=0;
g2=0;
r2=1;
y2=0;
g3=0;
r3=1;
y3=0;
g4=0;
r4=1;
y4=0;
n=n-1;
pause (1);
end
while ((p==1) && (n>0))
g1=0;
r1=0;
y1=1;
g2=0;
r2=1;
y2=0;
g3=0;
r3=1;
y3=0;
g4=0;
r4=1;
y4=0;
pause (1)
n=n-1;
end;
if (n==1)
a=2;
end
p = GLE;
p=s;

回答(2 个)

Steven Lord
Steven Lord 2016-11-30
The variable p isn't defined the first time you try to use it in your function, on the line "p = p+1;". What exactly do you think the result of that command should be?

Walter Roberson
Walter Roberson 2016-11-30
I speculate that you might want to start with
persistent p
if isempty(p)
p = 0;
end

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by