WHY do i get Error "Undefined function or variable"

3 次查看(过去 30 天)
As part of my code i run a while loop over a period of time defined and within it have this if loop which produces the array for xcounter, however if i try and use xcounter outside of the 'if' loop it says it is undefined?
Here is an extract of my code relevant to this and below it is the function file for counter;
time=0; %Initialise time
dt=0.01; %Define time step
tfinal=40;
diagstep=10; %define step size for diagnostic
diagcounter=0;
while time<tfinal
if ((round([time/diagstep]) * time) == time)
[xcounter] = ParticleCounterFun(x0, particleno)
diagcounter=diagcounter+1;
end
xcounter
end
The function file 'ParticleCounterfun' is as follows
function [ xcounter ] = ParticleCounterFun( x0, particleno )
%Initialise counters used in E field diagnostics
counter01=0;
counter12=0;
counter23=0;
counter34=0;
counter45=0;
for np=1:particleno
if 0<=x0(np) & x0(np)<1
counter01=counter01+1;
end
if 1<=x0(np) & x0(np)<2
counter12=counter12+1;
end
if 2<=x0(np) & x0(np)<3
counter23=counter23+1;
end
if 3<=x0(np) & x0(np)<4
counter34=counter34+1;
end
if 4<=x0(np) & x0(np)<5
counter45=counter45+1;
end
end
xcounter=[counter01 counter12 counter23 counter34 counter45];
end
Can someone tell me why when i run it matlab says;
>> ParticlePusher
Undefined function or variable 'xcounter'.
Error in ParticlePusher (line 100)
xcounter
Thank You!
  1 个评论
Star Strider
Star Strider 2014-2-21
It’s difficult for me to figure out what you’re doing, but it seems to me you could eliminate ParticleCounterFun altogether by using histc.

请先登录,再进行评论。

回答(2 个)

Mischa Kim
Mischa Kim 2014-2-21
编辑:Mischa Kim 2014-2-21
Phoebe, I assume that your code never enters the if-construct. If you remove the semi-colon from
diagcounter=diagcounter+1
do you see the value for diagcounter being displayed in the MATLAB command window? If not, the code does not enter the if-construct, xcounter does not get a value assigned and that's probably why you get the error msg.
  3 个评论
Phoebe
Phoebe 2014-2-23
Hi you are right I dont think it does enter my if loop but i cannot see why as if the time goes up to 40 and my diagstep is 10 you would expect to?
time=0; %Initialise time
tfinal=40;
diagstep=10;
diagcounter=0;
while time<tfinal
if ((round(time/diagstep) * diagstep) == time)
[xcounter] = ParticleCounterFun(x0, particleno)
diagcounter=diagcounter+1
end
Where the xcounter function file is;
function [ xcounter ] = ParticleCounterFun( x0, particleno )
%Initialise counters used in E field diagnostics
counter01=0;
counter12=0;
counter23=0;
counter34=0;
counter45=0;
for np=1:particleno
if (0<=x0(np)) & (x0(np)<1)
counter01=counter01+1;
end
if (1<=x0(np)) & (x0(np)<2)
counter12=counter12+1;
end
if (2<=x0(np)) & (x0(np)<3)
counter23=counter23+1;
end
if (3<=x0(np)) & (x0(np)<4)
counter34=counter34+1;
end
if (4<=x0(np)) & (x0(np)<5)
counter45=counter45+1;
end
end
xcounter=[counter01 counter12 counter23 counter34 counter45]
end
Mischa Kim
Mischa Kim 2014-2-23
Phoebe, there are a couple of issues (see code and comments). You probably have to fine-tune the code to make it do exactly what you want it to do. This should do, for now:
function test_fun()
time=0; %Initialise time
tfinal=40;
diagstep=10;
diagcounter=0;
xcounter = []; % initialize xcounter
x0 = [1 2 3]; % random x0
particleno = 3; % random particleno
while time<tfinal
if ((round(time/diagstep) * diagstep) == time)
xcounter = ParticleCounterFun(x0, particleno)
diagcounter = diagcounter+1
end
time = time + tfinal/diagstep; % update time for loop
end
end % end of test_fun()
function [ xcounter ] = ParticleCounterFun( x0, particleno )
counter01=0;
counter12=0;
counter23=0;
counter34=0;
counter45=0;
for np=1:particleno
if (0<=x0(np)) & (x0(np)<1)
counter01=counter01+1;
end
if (1<=x0(np)) & (x0(np)<2)
counter12=counter12+1;
end
if (2<=x0(np)) & (x0(np)<3)
counter23=counter23+1;
end
if (3<=x0(np)) & (x0(np)<4)
counter34=counter34+1;
end
if (4<=x0(np)) & (x0(np)<5)
counter45=counter45+1;
end
end
xcounter=[counter01 counter12 counter23 counter34 counter45];
end % end of PCF function

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2014-2-21
Perhaps you wanted
if ((round([time/diagstep]) * time) == time)
to be
if ((round([time/diagstep]) * diagstep) == time)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by