Matlab String Logic
显示 更早的评论
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.
回答(1 个)
Fangjun Jiang
2011-9-24
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.
3 个评论
Matthew
2011-9-24
Fangjun Jiang
2011-9-24
Yes. Check the correct syntax of if-elseif-else-end.
Also, use strcmpi() to compare the string.
Matthew
2011-9-24
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!