"Too many input arguments" when calling a constructor with named arguments
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I have a class which is a subclass of another class.
Superclass:
classdef fet
%FET transistor superclass
% defines a generic FET transistor.
properties
% fet SPICE
LEVEL, % SPICE simulation model
VTO, % threshold voltage with zero Vsb (unit: V)
GAMMA, % body-effect coefficient (unit: V^(1/2))
PHI, % 2*Phi_F i.e. workfunction (unit: V)
NSUB, % NSUB: substrate doping (unit: cm^(−3))
LD, % source/drain side diffusion (unit: m)
UO, % channel mobility (unit: cm^2/V/s)
LAMBDA, % channel-length modulation coefficient (unit: V^(−1))
TOX, % gate-oxide thickness (unit: m)
PB, % source/drain junction built-in potential (unit: V)
CJ, % source/drain bottom-plate junction capacitance per unit area (unit: F/m^2)
CJSW, % source/drain sidewall junction capacitance per unit length (unit: F/m)
MJ, % exponent in CJ equation (unitless)
MJSW, % exponent in CJSW equation (unitless)
CGDO, % gate-drain overlap capacitance per unit width (unit: F/m)
JS, % source/drain leakage current per unit area (unit: A/m^2)
% dimensions
W,
L,
% Oxide capacitance
Cox, % (unit: F/m^2)
% effective channel length
Leff,
% simulation properties
I_drain,
V_gs
V_ds
% device type ('NFET' or 'PFET')
type
% some constants
constants;
end
methods
function obj = fet(LEVEL, VTO, GAMMA, PHI, NSUB, LD, UO, LAMBDA, TOX, PB, CJ, CJSW, MJ, MJSW, CGDO, JS, W, L, type)
%Constructor
% constructs device parameters
constants = semiConstants;
obj.LEVEL = LEVEL;
obj.VTO = VTO;
obj.GAMMA = GAMMA;
obj.PHI = PHI;
obj.NSUB = NSUB;
obj.LD = LD;
obj.UO = UO;
obj.LAMBDA = LAMBDA;
obj.TOX = TOX;
obj.PB = PB;
obj.CJ = CJ;
obj.CJSW = CJSW;
obj.MJ = MJ;
obj.MJSW = MJSW;
obj.CGDO = CGDO;
obj.JS = JS;
obj.W = W;
obj.L = L;
obj.type = type;
obj.Cox = constants.epsilon_r_Si*constants.epsilon_0/obj.TOX;
obj.Leff = L-2*LD;
end
function outputArg = method1(obj,inputArg)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
outputArg = obj.Property1 + inputArg;
end
end
% % abstract functions
% methods (Abstract)
% Id_sat(obj)
% end
end
Subclass:
classdef nfet < fet
%NFET Summary of this class goes here
% Detailed explanation goes here
properties
end
methods
function obj = nfet(LEVEL, VTO, GAMMA, PHI, NSUB, LD, UO, LAMBDA, TOX, PB, CJ, CJSW, MJ, MJSW, CGDO, JS, W, L)
%NFET Constructor
% Constructs NFET
obj = obj@fet(LEVEL, VTO, GAMMA, PHI, NSUB, LD, UO, LAMBDA, TOX, PB, CJ, CJSW, MJ, MJSW, CGDO, JS, W, L, 'NFET');
end
function Id_sat(obj)
%METHOD1 computes drain current
obj.Id = 1/2*(UO*10^-4)*(obj.Cox)*(obj.W/obj.Leff)*(V_gs-VTO)^2*(1+(LAMBDA)*V_ds);
end
% function outputArg = method1(obj,inputArg)
% %METHOD1 Summary of this method goes here
% % Detailed explanation goes here
% outputArg = obj.Property1 + inputArg;
% end
end
end
When I call liket this:
nfet(1, 0.7, 0.45, 0.9, 9e14, 0.08e-6, 350, 0.1, 9e-9, 0.9, 0.56e-3, 0.35e-11, 0.45, 0.2, 0.4e-9, 1.0e-8, 50e-6, 0.5e-6)
It's all fine!
BUT, if I call like this:
nfet(LEVEL=1, VTO=0.7, GAMMA=0.45, PHI=0.9, NSUB=9e14, LD=0.08e-6, UO=350, LAMBDA=0.1, TOX=9e-9, PB=0.9, CJ=0.56e-3, CJSW=0.35e-11, MJ=0.45, MJSW=0.2, CGDO=0.4e-9, JS=1.0e-8, W=50e-6, L=0.5e-6)
I get an error:
Error using nfet
Too many input arguments.
What's going on? How to fix this? I want to call with names because if I want to change the value, I don't want to fiddle thru 18 arguments.
0 commentaires
Réponse acceptée
Rik
le 4 Mar 2022
Modifié(e) : Rik
le 8 Mar 2022
Your current syntax ( nfet(LEVEL=1) ) will be passed to your contructor as "LEVEL",1 (if you want a syntax that is compatible with older releases of Matlab, you should be using nfet('LEVEL',1) ). That means you have twice as many inputs as you thought.
You should implement your inputs as Name,Value pairs, for the exact reason you outline: more than a few positional arguments are simply too difficult to keep track of.
The function below is the one I'm currently using to parse Name,Value pairs. I use it for structs, but it should work the same for classes. Just input a struct with the default values and varargin{:}.
Edit: moved the file to an attachement. This update has more comprehensive comments and also solves the issue outlined in the comments, as the code snippet below shows.
parse_NameValue(struct('CJ',0,'CJSW',0), CJ=1, CJSW=2)
7 commentaires
Rik
le 8 Mar 2022
Your code suggestion breaks several test cases, so you may want to use the version in my updated answer instead. The passing tests all have the expected output.
try
parse_NameValue_Giorgi( struct('Parameter1',1,'Parameter2',2) , 'param',0 );
warning('test should have failed')
catch,end
try
parse_NameValue_Giorgi( struct('Param',1,'Parameter2',2) , 'param',0 )
catch,warning('test failed'),end
try
parse_NameValue_Giorgi( struct('x_mean',1) , 'XMEAN',0 )
catch,warning('test failed'),end
try
parse_NameValue_Giorgi( struct('x',0) , struct('x',{1,2}) )
catch,warning('test failed'),end
try
parse_NameValue_Giorgi( struct('x',{}) );
warning('test should have failed')
catch,end
try
parse_NameValue_Giorgi( struct('x',1) ,'y',0 );
warning('test should have failed')
catch,end
try
parse_NameValue_Giorgi( struct('x',1) , '',0 );
warning('test should have failed')
catch,end
try
parse_NameValue_Giorgi( struct('x',1) , "x",0 )
catch,warning('test failed'),end
Plus de réponses (1)
Matt J
le 4 Mar 2022
Modifié(e) : Matt J
le 4 Mar 2022
Note the attached utility copyprops().
function obj = fet(varargin)
constants = semiConstants;
obj=copyprops(struct(varargin{:}),obj);
obj.Cox = constants.epsilon_r_Si*constants.epsilon_0/obj.TOX;
obj.Leff = obj.L-2*obj.LD;
end
function obj = nfet(varargin)
%NFET Constructor
% Constructs NFET
obj = obj@fet(type='NFET',varargin{:});
end
0 commentaires
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!