MATLAB Answers


Matlab String Logic

Asked by Matthew
on 24 Sep 2011

I am working on a humidity calculator. Basically, I have all the equations needed to solve the problem, but my calculator requires a bit of supressing with % to select the right method.

For those not familiar with humidity percentage, phi, is a function of the mass of water in the air (omega), OR Dew point, OR Wet Bulb temperature.

I would like to have a function like: phi_finder(T,P,Dew Point OR Wet Bulb OR Omega)

where if Dew Point shows up in the function it executes equation 1 without me having to supress equation 2 and equation 3 manually.

I know this is a problem something I should be able to solve in the help file but I had no idea what to sarch for.


Log in to comment.

1 Answer

Answer by Fangjun Jiang on 24 Sep 2011

So your function will have three input arguments, T, P and one of Dew Point, or Wet Bulb, or Omega.

There is probably no easy way to tell whether the third input argument is Dew Point, or Wet Bulb, or Omega based on the value or data type. So I think you need to have a forth argument to specify it, like,

function Out=phi_finder(T,P,DPWBO,TypeStr), where TypeStr could be 'DewPoint', 'WetBulb', or 'Omega'. You could use any other type of identification as long as it is provided in the help text. You could also have a logic to say that if the forth input argument is not provided, that means the TypeStr is 'DewpOINT'.

help nargin
help varargin

Then inside the function, it's just a matter of checking the TypeStr and use different equation.


would it be something like:

if TypeStr = 'DP'
eqn 1
if TypeStr = 'WB'
eqn 2
if TypeStr = 'O'
eqn 3

Yes. Check the correct syntax of if-elseif-else-end.
Also, use strcmpi() to compare the string.

Thanks for the response. strcmpi() got me on my way.

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!