Variable 'a' is undefined on some execution paths.

7 visualizzazioni (ultimi 30 giorni)
when I start simulation, an error message 'Variable 'a_p' is undefined on some execution paths' arise and terminate simulation. I define the variable 'a_p' in that subsystem as following
for j = 1:J
if j==1;
a_p=[3,4,5,9,10];
else
if j==2;
a_p= [1,2,6,7,8,14,15];
else
if j==3;
a_p=[11 12 13 19 20];
else
if j==4;
a_p=[16 17 18 25];
else
if j==5;
a_p=[21 22 23 24];
end
end
end
end
end
end
It would be appreciated if you could help me on simulation running
  5 Commenti
Stephen23
Stephen23 il 23 Feb 2015
Modificato: Stephen23 il 23 Feb 2015
"Is it possible without for loop": yes, this is possible. You should:
  1. try the code that I gave you above, exactly and without any loops.
  2. tell us exactly the range of J.
  3. if something does not work, do not try to "fix" this by adding pointless loops into your code. Instead write us the exact error message or behavior that occurs.
  4. decide what behavior you want for cases of J outside of the integers one to five.
Saeed rhmt
Saeed rhmt il 23 Feb 2015
the range of J is 1 to 5; but j does not change during simulation on its own, so a_p will have one value all the time. and also the case of j outside the range of 1 to 5, won't happen.

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 22 Feb 2015
Modificato: Stephen23 il 22 Feb 2015
Both Geoff Hayes and Grieg have made some good points about simplifying the if statements, but it seems no one has addressed the point of the loop itself: if J is five for example this loop will run five times, and calls if a total of fifteen times, and yet the end result is simply that of j=5. This is slow and pointless.
Rather than doing this in unnecessary loops, why not use simpler, neater and faster code? switch would be the obvious choice:
switch J
case 1
a_p = [3,4,5,9,10];
case 2
a_p = [1,2,6,7,8,14,15];
case 3
a_p = [11,12,13,19,20];
case 4
a_p = [16,17,18,25];
case 5
a_p = [21,22,23,24];
otherwise
% error(...) ?
% a_p = [...] ?
end
  3 Commenti
Saeed rhmt
Saeed rhmt il 23 Feb 2015
Dear John, Many thanks for your help and suggestions. I tried it, but it did not work. the value of 'J' is also 5; Does this type of error depend on the Matlab version. I'm using Matlab R2013b.
Kind regard, Saeed
Stephen23
Stephen23 il 23 Feb 2015
Although you know what "but it did not work" means, we cannot read minds or your computer screen. Please tell us what the happened when you tried this, and we can fix it for you.

Accedi per commentare.

Più risposte (2)

Geoff Hayes
Geoff Hayes il 22 Feb 2015
Saeed - what is J? If J>5 then this error makes sense since you will only be defining a_p for j=1,2,3,4,5. You may want to default a_p to something before entering the for loop as
a_p = [1 2 3];
for j=1:J
% etc.
so that a_p is always defined. You may also want to simplify your above code and remove all of the else's. Why not try the following
for j=1:J
if j==1
a_p = [3,4,5,9,10];
elseif j==2
a_p = [1,2,6,7,8,14,15];
elseif j==3
a_p = [11 12 13 19 20];
elseif j==4
a_p = [16 17 18 25];
elseif j==5
a_p = [21 22 23 24];
else
error('invalid j (%d) - a_p undefined!\n',j);
end
end

Greig
Greig il 22 Feb 2015
It is really useful and much easier for folks to read code if you put it in Code formatting. Also, as good practice, I recommend writing your code well spaced and indented as it makes it much easier to read and identify problems. So let's do it here...
for j = 1:J
if j==1;
a_p=[3,4,5,9,10];
else
if j==2;
a_p= [1,2,6,7,8,14,15];
else
if j==3;
a_p=[11 12 13 19 20];
else
if j==4;
a_p=[16 17 18 25];
else
if j==5;
a_p=[21 22 23 24];
end
end
end
end
end
end
Firstly, I hope that structuring this with indentations has made it clearer and highlights a main problem with the code.
You have multiple nested if statements that would be much better written as a single if, elseif, else structure. Note I use "elseif" and not "else if". They are different. I suggest you look through
doc if
A better code structure would be...
for j = 1:J
if j==1
a_p=[3,4,5,9,10];
elseif j==2
a_p= [1,2,6,7,8,14,15];
elseif j==3
a_p=[11 12 13 19 20];
elseif j==4
a_p=[16 17 18 25];
elseif j==5
a_p=[21 22 23 24];
end
end
OK, so why doesn't it work? Well, what is the range of J? And what value does a_p take if J is, say 6? In this case a_p is undefined and this is probably what is causing your error. Replacing the last "elseif" with simply "else" should fix it, but you may need to check what values J takes.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by