Variable 'a' is undefined on some execution paths.

3 ビュー (過去 30 日間)
Saeed rhmt
Saeed rhmt 2015 年 2 月 22 日
コメント済み: Saeed rhmt 2015 年 2 月 23 日
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 件のコメント
Saeed rhmt
Saeed rhmt 2015 年 2 月 23 日
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.

サインインしてコメントする。

採用された回答

Stephen
Stephen 2015 年 2 月 22 日
編集済み: Stephen 2015 年 2 月 22 日
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 件のコメント
Stephen
Stephen 2015 年 2 月 23 日
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.

サインインしてコメントする。

その他の回答 (2 件)

Geoff Hayes
Geoff Hayes 2015 年 2 月 22 日
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 件のコメント
Saeed rhmt
Saeed rhmt 2015 年 2 月 23 日
Thank you Geoff for your help. Good Luck.

サインインしてコメントする。


Greig
Greig 2015 年 2 月 22 日
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.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by