WHY do i get Error "Undefined function or variable"
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Star Strider
le 21 Fév 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.
Réponses (2)
Mischa Kim
le 21 Fév 2014
Modifié(e) : Mischa Kim
le 21 Fév 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 commentaires
Mischa Kim
le 23 Fév 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
Walter Roberson
le 21 Fév 2014
Perhaps you wanted
if ((round([time/diagstep]) * time) == time)
to be
if ((round([time/diagstep]) * diagstep) == time)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!