필터 지우기
필터 지우기

set order of elseif

조회 수: 3 (최근 30일)
Rub Ron
Rub Ron 2020년 12월 17일
댓글: James Tursa 2020년 12월 17일
Updated.
I have this code structure inside a loop. x, y and z are 1/0 variables. In this case the order of the elseif statement is: first x is evaluated, then y then z
if x
%code for case x
elseif y
%code for case y
elseif z
%code for case z
end
I would like to define a varible order (for the previous case order={'caseX','caseY','caseZ'}), where I can change the order in which the elseif statement is evaluated. So if I set order={'caseZ','caseX','caseY'}, I should get this: (first z is evaluated, then x then y)
order={'caseZ','caseX','caseY'}
%%% then
if z
%code for case z
elseif x
%code for case x
elseif y
%code for case y
end
An hint will be appreciated.
  댓글 수: 2
Ive J
Ive J 2020년 12월 17일
편집: Ive J 2020년 12월 17일
I'm not sure if I understood you correctly, but seems easier if you use switch/case. See here:
KSSV
KSSV 2020년 12월 17일
Have a look on switch.

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

채택된 답변

James Tursa
James Tursa 2020년 12월 17일
편집: James Tursa 2020년 12월 17일
Does this do what you want:
order = [1 2 3]; % or whatever
for k=1:numel(order)
switch order(k)
case 1
if( x )
% code for case x
break;
end
case 2
if( y )
% code for case y
break;
end
case 3
if( z )
% code for case z
break;
end
end
Or if you prefer the case selection could be for characters 'x', 'y', and 'z' instead of numbers 1, 2, 3.
The key is to have those break statements inside the cases so that the if-elseif-elseif control structure is emulated properly.
  댓글 수: 9
Rub Ron
Rub Ron 2020년 12월 17일
Thanks Bruno, I wish I can accept both answers at the same time, with only one click
James Tursa
James Tursa 2020년 12월 17일
I gave Bruno a vote since he contributed a key part of the solution.

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

추가 답변 (2개)

Bruno Luong
Bruno Luong 2020년 12월 17일
prefered_order = 'xzy';
b = [x y z]; % logical conditions (in if/elseif)
code_to_be_exec = { @xcode, @ycode, @zcode }; % code in the if elseif block put in functions
[~,i] = ismember(prefered_order, 'xyz');
j = find(b(i), 1, 'first');
if ~isempty(j)
feval(code_to_be_exec{i(j)});
end
  댓글 수: 2
Rub Ron
Rub Ron 2020년 12월 17일
do you think there is a way to preseve the if elseif structure? for easy-to-read purpose.
Bruno Luong
Bruno Luong 2020년 12월 17일
편집: Bruno Luong 2020년 12월 17일
Of course, unroll all 6 possible permutations
if strcmp(prefered_order, 'xyz')
% your original if/else
elseif strcmp(prefered_order, 'yxz')
% another order...
elseif strcmp(prefered_order, 'zyx')
....
end

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


KSSV
KSSV 2020년 12월 17일
Are you looking something like this.
if z
%code for case z
order = ['case','z'] ;
elseif x
%code for case x
order = ['case','x'] ;
elseif y
%code for case y
order = ['case','y'] ;
end
  댓글 수: 3
KSSV
KSSV 2020년 12월 17일
order = 'z'
if strcmpi(order,'z')
%code for case z
order = ['case','z'] ;
elseif strcmpi(order,'x')
%code for case x
order = ['case','x'] ;
elseif strcmpi(order,'y')
%code for case y
order = ['case','y'] ;
end
Also have a look on switch.
Rub Ron
Rub Ron 2020년 12월 17일
I updated my post, hopefully now it is clearer.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by