Use of nargin and varargin

40 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-6-26
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 2019-6-26
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.

请先登录,再进行评论。

采纳的回答

per isakson
per isakson 2019-6-27
编辑:per isakson 2019-6-27
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

产品


版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by