Use of nargin and varargin
Show older comments
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.
- Area horizontal + Diameter inside (sometimes like this also possinle: Area horizontal + diameter inside + diameter outside)
- Area horizonatl + diameter outside
- Area vertical + diameter insie (sometimes like this also possinle: Area Vertical + diameter inside + diameter outside)
- 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
Walter Roberson
on 26 Jun 2019
if nargin < 2 || isempty(A_Vertical)
A_Vertical = 0;
end
if nargin < 3 || isempty(d_outside)
d_outside = 0;
end
Bjorn Gustavsson
on 26 Jun 2019
To an outside observer this seems like a case of mission-creep. Perhaps you could use the name-value pair to make the function capable to parse what the inputs are - this seems like a very heavyhanded way to call this type of function to me. You could use the expectation of four input parameters, check which ones of them are empty and handle all combinations (making sure to throw errors and handle default behaviours for input combinations that's outside what you want/expect, and document all of this) - this will make it cumbersome to remember which arguments are in what position in the call and.
I'd (perhaps) make a function taking suggestion 2 and add a flag for what case to handle, then write user-level functions for each case, LL_Leitwert_freie_Konvektion_Strahlung_VAID etc, that only calls the core-function with the corresponding flag - also not a good design.
surendra kumar Aralapura mariyappa
on 26 Jun 2019
Edited: per isakson
on 26 Jun 2019
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
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.
Accepted Answer
More Answers (0)
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!