Variable 'a' is undefined on some execution paths.

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

Woooow.. I solved it. You must define a_p as an empty vector before allocating a vector to it as follow. since it's dimension is changing continuously.
for j=1:J
a_p=[]
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
end
if true
% code
end
But after a few seconds running, the following message appears. what should I do?
''The working dimension was selected automatically, is variable-length, and has length 1 at run-time. This is not supported. Manually select the working dimension by supplying the DIM argument.''
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.
Thanks for your comments. Maybe you're right, but I could solve the problem by define ''a_p=[]'' before switch. for using for loop, I should say that I want all values of a_p for ranges j=1 to 5 and with only 'switch' and without for loop, I can'd do that. Is it possible without for loop?
Regards,
"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.
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.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 22 Feb 2015
Edited: Stephen23 on 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 Comments

+! I was reading through the various responses, and wondering if anyone was going to say what seemed the obvious - why not just use a switch and discard the loop completely?
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
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.

Sign in to comment.

More Answers (2)

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
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

Tags

Asked:

on 22 Feb 2015

Commented:

on 23 Feb 2015

Community Treasure Hunt

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

Start Hunting!