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!