MATLAB Answers

Use of nargin and varargin

85 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;
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


Show 2 older comments
surendra kumar Aralapura mariyappa
Hello Roberson,
Thank you for the quick response.
In my question, I have mentioned, nearly 6 possibilites,
But with you answer I don't think so all the conditions are possible. For an example.
if nargin < 2 || isempty(A_Vertical)
A_Vertical = 0;
if nargin < 3 || isempty(d_outside)
d_outside = 0;
There are two equations inside the function. one for A_Vertical and A_Horzontal which contains both d_outside and d_inside.
if user gave A_Vertical and d_outside, now A_Horizontal should be zero and d_inside should also be zero, since he didn't give d_inside.
If user gave A_Vertical, d_outside and d_inside, A_Horizontal should be zero.
if user gave A_Vertical and d_insde, A_Horizontal should be zero and d_outside should be zero
if user gave A_Horizontal and d_outside, A_Vertical should be zero and also d_inside should be zero.
if user gave A_Horizontal and d_inside, A_Vertical should be zero and also d_outside should be zero.
If user gave A_Horizontal, d_outside and d_inside, A_Vertical should be zero.
would it be possible to do the above task with your solution!
Would it works if I do like below
if nargin <= 2 && isempty(A_Vertical) && isempty(d_outside)
A_Vertcial = 0;
d_outside = 0;
if nargin <= 2 && isempty(A_Vertical) && isempty(d_inside)
A_Vertcial = 0;
d_inside = 0;
if nargin <= 3 && isempty(A_Vertical)
A_Vertcial = 0;
if nargin <= 2 && isempty(A_Horizontal) && isempty(d_outside)
A_Horizontal= 0;
d_outside = 0;
if nargin <= 2 && isempty(Horizontal) && isempty(d_inside)
A_vertcial = 0;
d_inside = 0;
if nargin <= 3 && isempty(A_Horizontal)
A_Horizontal = 0;
Please let me know!
Thanks in advance
Walter Roberson
Walter Roberson on 26 Jun 2019
Just extend what I gave to cover the other two inputs as well.
After that, you might want to veto some combinations if too many of the inputs were 0 -- which you would need to do even if the user specifically entered the input but entered it as 0.
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 };
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
  • 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

  1 Comment

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!