Switch skipping over cases in new loop
조회 수: 3 (최근 30일)
이전 댓글 표시
Evening everyone,
I have a strange problem. In the code below, switch is recongising all conditoins in respect to "qq" within its loop and changing the variable value accordingly. However, the second switch "pp" it will only recongise the condition for the first loop only, after which the same variable is used i.e., it assesses the cases and skips over them. I have tried changing the case brackets from { } to ( ), clearing the counter (pp) to force it reload the variable in respect to the "i" loop and deleting the variable itself. None of this is working and I am stumped.
I do not have any data to provide because it is too large. Any ideas?
Thanks
for i=1:numel(H) % 9 figures
ax=findobj(H(i),'Type','axes');
for j=1:numel(ax)
leg_tit_up = sprintf('Slope: %s%s',SA_type,char(176));
% Nasty fix
qq = i; % Changed to qq to try and get second switch working
switch(qq)
case{1,4,7}
cell_trans_idx = cell_trans_flip{1};
case{2,5,8}
cell_trans_idx = cell_trans_flip{2};
case{3,6,9}
cell_trans_idx = cell_trans_flip{3};
end
%clear switch
clear qq;
pp = i; % Only works after one conditon then bypasses to end - keeping the initial "leg_tit_dwn" value
switch(pp)
case{pp>7}
leg_tit_dwn =Water_levels(1) ;
case{pp>3 && pp<7}
leg_tit_dwn =Water_levels(2) ;
case{pp<4}
leg_tit_dwn =Water_levels(3) ;
end
clear pp
%clear switch
leg_tit = {leg_tit_up;leg_tit_dwn};
lgd = legend(ax(j),Cell_ID(cell_trans_idx),'fontweight','bold','box','on','location','bestoutside');
title(lgd,leg_tit);
clear leg_tit_dwn;
end
end
댓글 수: 0
채택된 답변
Walter Roberson
2021년 7월 10일
You have
pp = i; % Only works after one conditon then bypasses to end - keeping the initial "leg_tit_dwn" value
switch(pp)
case{pp>7}
leg_tit_dwn =Water_levels(1) ;
case{pp>3 && pp<7}
leg_tit_dwn =Water_levels(2) ;
case{pp<4}
leg_tit_dwn =Water_levels(3) ;
end
That code is effectively
if ismember(pp, [pp>7])
leg_tit_dwn =Water_levels(1) ;
elseif ismember(pp, [pp>3 && pp<7])
leg_tit_dwn =Water_levels(2) ;
elseif ismember(pp, [pp<4])
leg_tit_dwn =Water_levels(3) ;
end
Notice this can only be true if pp is 0 or 1.
If you must use switch() instead of if/elseif, then change the
switch(pp)
to
switch(true)
and then you would be doing the equivalent if ismember(true, pp>7) which is at least possible.
댓글 수: 2
Walter Roberson
2021년 7월 11일
Question: why do you permit pp>3 and pp<4 to overlap ? For example if pp is 3.1 then it is both > 3 and < 4 so a reader would be justified in having confusion over which value applies.
추가 답변 (1개)
DGM
2021년 7월 10일
편집: DGM
2021년 7월 10일
That's not how switch-case statements work. The case is only entered when the switch expression (pp is numeric) is equal to the case expression (which are logical). Just use if-else structures here.
% simplified for demonstration
i = 5
switch i
case {1,4,7}
cell_trans_idx = 1
case {2,5,8}
cell_trans_idx = 2
case {3,6,9}
cell_trans_idx = 3
end
% what happens when i = 7?
if i>7
leg_tit_dwn = 1
elseif i>3 && i<7
leg_tit_dwn = 2
elseif i<4
leg_tit_dwn = 3
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!