From: venkat on
In the below given class it works for: obj1 = classA('string', 'hai')
but I wiould like to also use it as: obj2 = classA('str', 'hai') or
obj3 = classA('stri', 'hai')
i.e using short form of parser parameters I want to run my class.
Using 'str' or 'stri' or 'strin' etc must work as similar to using
'string' in the arguments. Can any one suggest me on this??
like using 'str', 'pos' in uicontrol instead of 'string', 'position'
etc..
uicontrol('str', 'hello', 'pos', [10 10 50 20])


****************************
classdef classA
properties
string
enable
fontsize
end
methods
function obj = classA(varargin)
p=inputParser;

p.addParamValue('string', 'New String', @ischar);
p.addParamValue('enable', true, @islogical);
p.addParamValue('fontsize', 10, @isnumeric);

p.parse(varargin{:});

obj.string = p.Results.string;
obj.enable = p.Results.enable;
obj.fontsize = p.Results.fontsize;

end
end
end
From: Matthew Whitaker on
One approach would be to subclass the inputParser to preprocess the PV values. For example:

classdef inputParserLazy <inputParser

methods
function obj = inputParserLazy
obj = obj(a)inputParser;
end %inputParserLazy


function parse(obj,arglist)
%NOTE: As written here we are assuming ONLY PV pairs
param = obj.Parameters;
pList = arglist(1:2:end);
for p = 1:length(pList)
match = strmatch(pList{p},param);
if ~isempty(match)
arglist{p*2-1} = param{match(1)};
end %if
end %for
parse(a)inputParser(obj,arglist{:});
end %parse

end %methods
end %

then:
p = inputParserLazy;
p.addParamValue('string', 'New String', @ischar);
p.addParamValue('enable', true, @islogical);

%pass pv the normal way
pv = {'string','A String','enable',false};
p.parse(pv)
p.Results

%pass PV the abbreviated way
pv = {'str','A String','en',false};
p.parse(pv)
p.Results

ans =

enable: 0
string: 'A String'

ans =

enable: 0
string: 'A String'



There are probably several ways to do this.

Hope this Helps
Matt W






venkat <venky.iitd(a)gmail.com> wrote in message <2d728a38-8d75-4862-83e2-677fb0db2fd9(a)d20g2000yqh.googlegroups.com>...
> In the below given class it works for: obj1 = classA('string', 'hai')
> but I wiould like to also use it as: obj2 = classA('str', 'hai') or
> obj3 = classA('stri', 'hai')
> i.e using short form of parser parameters I want to run my class.
> Using 'str' or 'stri' or 'strin' etc must work as similar to using
> 'string' in the arguments. Can any one suggest me on this??
> like using 'str', 'pos' in uicontrol instead of 'string', 'position'
> etc..
> uicontrol('str', 'hello', 'pos', [10 10 50 20])
>
>
> ****************************
> classdef classA
> properties
> string
> enable
> fontsize
> end
> methods
> function obj = classA(varargin)
> p=inputParser;
>
> p.addParamValue('string', 'New String', @ischar);
> p.addParamValue('enable', true, @islogical);
> p.addParamValue('fontsize', 10, @isnumeric);
>
> p.parse(varargin{:});
>
> obj.string = p.Results.string;
> obj.enable = p.Results.enable;
> obj.fontsize = p.Results.fontsize;
>
> end
> end
> end