Borrar filtros
Borrar filtros

WHY do i get Error "Undefined function or variable"

3 visualizaciones (últimos 30 días)
Phoebe
Phoebe el 21 de Feb. de 2014
Comentada: Mischa Kim el 23 de Feb. de 2014
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 comentario
Star Strider
Star Strider el 21 de Feb. de 2014
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.

Iniciar sesión para comentar.

Respuestas (2)

Mischa Kim
Mischa Kim el 21 de Feb. de 2014
Editada: Mischa Kim el 21 de Feb. de 2014
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 comentarios
Phoebe
Phoebe el 23 de Feb. de 2014
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 el 23 de Feb. de 2014
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

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 21 de Feb. de 2014
Perhaps you wanted
if ((round([time/diagstep]) * time) == time)
to be
if ((round([time/diagstep]) * diagstep) == time)
  1 comentario
Phoebe
Phoebe el 21 de Feb. de 2014
yes that is correct and I have amended it although the same error still appears?

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by