Switch skipping over cases in new loop
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
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
Categorías
Más información sobre Cell Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!