Trouble using switch case

4 ビュー (過去 30 日間)
Scotty
Scotty 2013 年 1 月 9 日
Below is example code. I am running into problems when theState becomes 'ACTIVE.' I am looking to see it stay in that 'ACTIVE' state until criteria is met. Thus it should fprintf('Still Active'),... well in my world at least :).
What am I missing here, is it problematic to use switch/case inside of switch/case. Or is there a massive user error!
% theState = 'INACTIVE'; this is where we start. In 'INACTIVE'
x = 1; y = -0.5;
for i = 1:100
switch theState
case 'INACTIVE'
if x == 1
%yadaYada
theState = 'ACTIVE';
whereActive = 'kramer';
fprintf('ACTIVE kramer \n');
elseif x == 2
%yadaYada
theState = 'ACTIVE';
whereActive = 'george';
fprintf('ACTIVE george \n');
end
case 'ACTIVE'
switch whereActive
case 'kramer'
if y >= 1
%yadaYada
theState = 'INACTIVE';
fprintf('INACTIVE by kramer');
end
case 'george'
if y <= -1
%yadaYada
theState = 'INACTIVE';
fprintf('INACTIVE by george');
end
otherwise
fprintf('Still Active');
end
end
end

採用された回答

Jan
Jan 2013 年 1 月 9 日
編集済み: Jan 2013 年 1 月 9 日
This is a job for the line-by-line debugging. Set a breakpoint in each line, which changes or tests the internal states. Then run the program agian and observe how your system is developping.
Computers are not smart. But in running a program step by step they are really and absolutely perfect. Therefore it is much more efficient, when you do this locally instead of letting members of the forum doing this remotely without having the full code.
  2 件のコメント
Scotty
Scotty 2013 年 1 月 9 日
Jan,
Thanks you always have good feedback.
I actually did that (i rarely ask question here) but only with breakpoints at specific areas. I will look to put in several... however the logic of the code is there.
consider when in an active state stay in that state until criteria is met. Then return to inactive... is what i am trying to get at.
Scotty
Scotty 2013 年 1 月 10 日
Got it thanks Jan!
I stepped through it with about 30 breakpoints..took a bit but it worked.

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

その他の回答 (2 件)

Matt J
Matt J 2013 年 1 月 9 日
編集済み: Matt J 2013 年 1 月 9 日
A few observations
  1. You never change whereActive to anything other than kramer or george, so the "otherwise" block where fprintf('Still Active') lies never gets executed.
  2. You never change y to satisfy y<=-1 or y>=1, so the cases 'kramer' and 'george' under 'ACTIVE' never do any work
  3 件のコメント
Matt J
Matt J 2013 年 1 月 9 日
編集済み: Matt J 2013 年 1 月 9 日
Ok then why should it NOT tell me the 'theState' is still 'ACTIVE' even though the criteria for y has not been met?
Because you have criteria for both y and whereActive, which need to be simultaneously satisfied in order for something to happen. They never are, however.
But when you change y to y = -1.5 you will still not see it exit the 'ACTIVE'
As a case in point, y=-1.5 will only trigger something if whereActive='george'. That never happens.
Scotty
Scotty 2013 年 1 月 10 日
Oh god... my dyslexia is kicking in... I solved my problem it works as intended.
thanks.

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


Thorsten
Thorsten 2013 年 1 月 9 日
I think you can fix it if you put the "if y " first in the 'ACTIVE' case and then switch depending on whereActive
case 'ACTIVE'
if abs(y) < 1
fprint('Still active')
else
switch whereActive
case 'kramer'
if y >= 1
%yadaYada
theState = 'INACTIVE';
fprintf('INACTIVE by kramer');
end
case 'george'
if y <= -1
%yadaYada
theState = 'INACTIVE';
fprintf('INACTIVE by george');
end
end
end % if abs(y) < 0.5

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by