From: Janez Korosec on
Good day,

I have a function, based on dynamic fields, which copies all properties from an input struct to "this" object. I have a whole collection of classes where I want to use it. I can't use inheritance since some properties are protected or private.

One solution is to copy that function onto each "@" folder. I don't have to mention that this is rather cumbersome workaround.

1. Is there any way to make some arbitrary function to act a member method in many classes.

2. Is there a way to change access type of property belonging to an object passed to superclass.

3. Any other ideas?
From: per isakson on
"Janez Korosec" <mato2k(a)gmail.com> wrote in message <hvsvq4$if6$1(a)fred.mathworks.com>...
> Good day,
>
> I have a function, based on dynamic fields, which copies all properties from an input struct to "this" object. I have a whole collection of classes where I want to use it. I can't use inheritance since some properties are protected or private.
>
> One solution is to copy that function onto each "@" folder. I don't have to mention that this is rather cumbersome workaround.
>
> 1. Is there any way to make some arbitrary function to act a member method in many classes.
>
> 2. Is there a way to change access type of property belonging to an object passed to superclass.
>
> 3. Any other ideas?

I use an ordinary function to achieve the behaviour that I think you describe

function this = ipv2this( this, ipv )
caNames = transpose( intersect( properties( this ), fieldnames( ipv ) ) );
for ca = caNames
this.( ca{:} ) = ipv.( ca{:} );
end
end

/per
From: per isakson on
"per isakson" <poi.nospam(a)bimDOTkthDOT.se> wrote in message <i0nmts$noc$1(a)fred.mathworks.com>...
> "Janez Korosec" <mato2k(a)gmail.com> wrote in message <hvsvq4$if6$1(a)fred.mathworks.com>...
> > Good day,
> >
> > I have a function, based on dynamic fields, which copies all properties from an input struct to "this" object. I have a whole collection of classes where I want to use it. I can't use inheritance since some properties are protected or private.
> >
> > One solution is to copy that function onto each "@" folder. I don't have to mention that this is rather cumbersome workaround.
> >
> > 1. Is there any way to make some arbitrary function to act a member method in many classes.
> >
> > 2. Is there a way to change access type of property belonging to an object passed to superclass.
> >
> > 3. Any other ideas?
>
> I use an ordinary function to achieve the behaviour that I think you describe
>
> function this = ipv2this( this, ipv )
> caNames = transpose( intersect( properties( this ), fieldnames( ipv ) ) );
> for ca = caNames
> this.( ca{:} ) = ipv.( ca{:} );
> end
> end
>
> /per

...., which works fine with public properties - only!

/ per