Variable 'a' is undefined on some execution paths.
Show older comments
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 Comments
Saeed rhmt
on 23 Feb 2015
Your statement "You must define a_p as an empty vector before allocating a vector to it" is wrong: MATLAB does not require variables to be declared before assigning to them. And certainly in your suggested "solution" it is completely unnecessary, as you can simply define and assign to a_p simultaneously within an assignment statement.
Your suggested "solution" still uses a for loop, which is not necessary and just makes your code more complicated than it needs to be. Remove the for loop, it actually does nothing to help you at all. Use the code I gave, without any for loop.
The most important question which you have been asked twice but not yet answered: What is the range of J? This determines whether you need to use the otherwise case, because we can use the otherwise case within the switch statement to define what happens if J>5:
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(...) ? <- Choose this if you want an error message
% a_p = [...] ? <- Choose this if you want to define an a_p value
end
The error message that you are getting now seems to be unrelated to this switch statement, but unless you actually give the whole error message and the code it refers to it will be impossible to diagnose.
Saeed rhmt
on 23 Feb 2015
"Is it possible without for loop": yes, this is possible. You should:
- try the code that I gave you above, exactly and without any loops.
- tell us exactly the range of J.
- 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.
- decide what behavior you want for cases of J outside of the integers one to five.
Saeed rhmt
on 23 Feb 2015
Accepted Answer
More Answers (2)
Geoff Hayes
on 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
1 Comment
Saeed rhmt
on 23 Feb 2015
Greig
on 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.
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!