Use of nargin and varargin

39 views (last 30 days)
Hey all,
Hope everyone is doing good. I need some help in fixing out my problem. I have a function which is shown below. Sometimes user give more inputs or less inputs But here there are four combinations of giving the input like below.
  1. Area horizontal + Diameter inside (sometimes like this also possinle: Area horizontal + diameter inside + diameter outside)
  2. Area horizonatl + diameter outside
  3. Area vertical + diameter insie (sometimes like this also possinle: Area Vertical + diameter inside + diameter outside)
  4. Area vertical + diameter inside
This is my function;
function [ L_ges ] = LL_Leitwert_freie_Konvektion_Strahlung( A_Horizontal ,A_Vertical,d_outside,d_Inside)
% Detailed explanation goes here
d_outside = d_outside/1000; % Umrechnung in m
d_inside = d_Inside/1000; % Umrechnung in m
A_Horizontal = A_Horizontal/1000/1000; % Umrechnung in m^2 Horizontal area
A_Vertical = A_Vertical/1000/1000; % Umrechnung in m^2
d_WK_M = d_Innen+b_Bauteil/2;
end
Still function has further calculations, I think it is not needed now.
Here what I needed is, When the user enter first combination input arguments, diameter outside(shouldn't be zero sometimes) and area vertical should be zero. Similary for vertical outside and vertical inside,
i think it is possible using nargin or varargin like below, But I am not getting how to do it.
It is like making some arguments zero according to the user input. Here also I am not sure, whether it is possible or not!.
if there another way of sloving it, that is also most welcomed.
Any suggestions and answers are most welcomed
Thanks in advance
  5 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 26 Jun 2019
Yes, it would. You just have to sit down and map out all combinations of input variable and what should happen with each combination. Once you've done that you can implement that in a number of ways. You might first parse the input parameters and decide which case you have at hand and then set the necessary remaining parameters to what they should be and call the appropriate function for that case. By my count you ought to have approximately 3 different combinations of areas: A_V, A_H and A_V and A_H, and three different combinations of diametres: D_I, D_O, D_I and D_O, that would make 9 possible combinations of input parameters, some of which you won't like. So just get to it.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 27 Jun 2019
Edited: per isakson on 27 Jun 2019
It's a bit of a challenge to make this program
  • correct
  • easy to maintain
  • user friendly
(I assume that you really need all these combinations of inputs.)
My approach would be
function varargout = LL_Leitwert_freie_Konvektion_Strahlung( varargin )
p = inputParser();
p.KeepUnmatched = false;
p.CaseSensitive = false;
p.StructExpand = false;
p.PartialMatching = true;
addParameter( p, 'HorizontalArea' , nan ) % I like NaN as value of
addParameter( p, 'VerticalArea' , nan ) % variables that must not
addParameter( p, 'OutsideDiameter' , nan ) % be used. NaN makes use
addParameter( p, 'InsideDiameter' , nan ) % by mistake obvious.
parse( p, varargin{:} )
ip = p.Results;
fprintf( '%16s %s\n', 'name' , 'value' )
fprintf( '%16s = %f\n', 'HorizontalArea' , ip.HorizontalArea )
fprintf( '%16s = %f\n', 'VerticalArea' , ip.VerticalArea )
fprintf( '%16s = %f\n', 'OutsideDiameter', ip.OutsideDiameter )
fprintf( '%16s = %f\n', 'InsideDiameter' , ip.InsideDiameter )
% parameters not in the input string
is_case_1 = isempty( setxor( {'VerticalArea' ,'OutsideDiameter'}, p.UsingDefaults ));
is_case_2 = isempty( setxor( {'VerticalArea' ,'InsideDiameter' }, p.UsingDefaults ));
is_case_3 = isempty( setxor( {'VerticalArea' }, p.UsingDefaults ));
is_case_4 = isempty( setxor( {'HorizontalArea','OutsideDiameter'}, p.UsingDefaults ));
is_case_5 = isempty( setxor( {'HorizontalArea','InsideDiameter' }, p.UsingDefaults ));
is_case_6 = isempty( setxor( {'HorizontalArea' }, p.UsingDefaults ));
assert( any( [is_case_1,is_case_2,is_case_3,is_case_4,is_case_5,is_case_6] )...
, 'abc:def:IllegalInputCombination' ...
, 'Your combination of inputs is illegal' )
% further calculations
varargout = { ip, p };
end
and a test
>> [ip,p] = LL_Leitwert_freie_Konvektion_Strahlung( 'HorizontalArea',1, 'o',3 );
name value
HorizontalArea = 1.000000
VerticalArea = NaN
OutsideDiameter = 3.000000
InsideDiameter = NaN
>>
and a test with four input parameters, which is illegal
>> [ip,p] = LL_Leitwert_freie_Konvektion_Strahlung( 'HorizontalArea',1, 'o',3, 'i',4, 'v',2 );
name value
HorizontalArea = 1.000000
VerticalArea = 2.000000
OutsideDiameter = 3.000000
InsideDiameter = 4.000000
Error using LL_Leitwert_freie_Konvektion_Strahlung (line 30)
Your combination of inputs is illegal
Comments
  • The user has to type more keystrokes, but doesn't need to remember the order of the different cases.
  • Using the full input names improve readability.
  • is_case_1 a much better name is needed
  • isempty(setxor({'VerticalArea','OutsideDiameter'},p.UsingDefaults )); It would be easier to grasp an expression based on input parameters

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!