MATLAB Answers

Kofial
0

How can I use switch case function to get decimal numbers?

Asked by Kofial
on 7 Nov 2019
Latest activity Commented on by Steven Lord
on 8 Nov 2019
M% Temperatur_C = [13,6169 14,5432 15,2356 17,2456 18,1887 19.9874 21,2765]
% Coeff -30C -20 -10 0 10 20 30 40 50
% nT 1.8 1.8 1.4 1.1 1.1 1 0.9 0.9 0.8
for A=1:(a-1)
switch (Temperatur_C(1,A))
case num2cell(-10.0:0)
disp('nT=1.4')
nT_NO(1,A) = 1.4;
case num2cell(0.1:4.99)
disp('nT=1.1')
nT_NO(1,A) = 1.1;
end
If I use round command I get the answer but that's not right for me because I want to be able to get the specific temperature not rounded.For example if the temp is 18,1887 it gets it as 20.
for A=1:(a-1)
switch round(Temperatur_C(1,A))
case num2cell(-10.0:0)
disp('nT=1.4')
nT_NO(1,A) = 1.4;
case num2cell(0.1:4.99)
disp('nT=1.1')
nT_NO(1,A) = 1.1;
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Guillaume
on 7 Nov 2019
 Accepted Answer

I'm curious if you understand what that num2cell(-10.0:0) actually do in the case statement? If you don't and it's something you've been given by someone else don't use it.
It's not clear exactly what you're trying to do. I suspect it is this (minus the disps which are a bit pointless):
Breakpoints = [-Inf -30 -10 0 20 30 50 +Inf];
nT_Values = [NaN 1.8 1.4 1.1 1 0.9 0.8];
whichcolumn = discretize(Temperatur_C, Breakpoints);
nT_NO = nT_Values(whichcolumn);
%and if you want the equivalent to disp:
%celldisp(compose('nT=%g', nT_NO));
You can't do what you were trying to do with switch statements, you could do it with if...elseif
for idx = 1:numel(Temperature_C)
if Temperatur_C(idx) >= -30 & Temperatur_C(idx) < -10
nT_NO(idx) = 1.4;
elseif Temperatur_C(idx) >= -10 & Temperatur_C(idx) < 0
nT_NO(idx) = 1.1;
elseif ...etc
end
end
As you can see, using discretize is much simpler.

  4 Comments

Show 1 older comment
Call discretize to determine which bin each element of Temperatur_C is in. Have a vector of nT values for each gas. Index into that vector of nT values for each gas using the bin numbers.
This requires two lines for the temperature data (one to define the data, one to discretize the data) and two lines per gas (one to define the nT values, one for the indexing.)
Yes, as I've explained use discretize it's so much simpler that your two loops with endless elseif, plus the discretize call, as I wrote it, has the advantage that you get defined behaviour even if the temperature is outside your defined [-15, 45] range. With your code, nothing is ever assigned to the output variables (which may cause error later on as the variable may not even exist).
Your code can be simplified to:
breakpoints = [-Inf, -15, -5, 5, 15, 25, 35, 45, +Inf]; %temperature break points
nT_OX_Coeff = [ NaN, 1, 1.3, 1.5, 1.7, 2, 2.5, NaN];
nT_CO_Coeff = [ NaN, 1.4, 1.1, 1.1, 1.1, 0.9, 0.9, NaN];
nT_OX = discretize(Temperatur_C, breakpoints, nT_OX_Coeff);
nT_NO = discretize(Temperatur_C, breakpoints, nT_CO_Coeff);
%all done. No loop, no endless if...else...elseif
edit: lots of typos!
In addition to the advantage Guillaume cited about what happens outside the bounds, this way if you need to add additional breakpoints (and the corresponding nT coefficients) you only need to modify the lines where the data was created. You don't need to modify the code at all (and run the risk of updating the large if / else / elseif / end statement for one gas but not the other.)

Sign in to comment.