Switch skipping over cases in new loop

3 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jul 2021
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 Comments
Walter Roberson
Walter Roberson on 11 Jul 2021
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.
DGM
DGM on 11 Jul 2021
Also, pp=7 is a case which is not handled at all.

Sign in to comment.

More Answers (1)

DGM
DGM on 10 Jul 2021
Edited: DGM on 10 Jul 2021
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!