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
Stephen23
Stephen23 2015년 2월 23일
편집: Stephen23 2015년 2월 23일
"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 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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2015년 2월 22일
편집: Stephen23 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
Saeed rhmt
Saeed rhmt 2015년 2월 23일
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 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

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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by