MATLAB Answers

Creating a new object from existing object but after excluding one specific property.

1 view (last 30 days)
Deepak Gupta
Deepak Gupta on 11 Aug 2020
Edited: Walter Roberson on 12 Aug 2020
Hi All,
I want to copy an object from exsiting object but don't want to copy all the properties, more specifically one perticular property.
Problem i am facing is that i don't know all the perperties exisiting in the object from which i am copying. But if there is this one property in this object, i want to exclude it and copy rest of the object to the new object. For example i have obj1:
function obj1 = obj1
obj1.A = 10;
obj1.B = 20;
obj1.C = 30;
And now i copy this object to a new obj:
x = obj1
y = obj2(x)
function obj2 = obj2(x)
obj2 = x(x.*!=x.C)
In second part of code, line number 5 has syntax error on purpose to represent what i want. I


Sign in to comment.

Accepted Answer

J. Alex Lee
J. Alex Lee on 11 Aug 2020
If you actually mean structures, as your example suggests, are you aware of "isfield", which will check if a structure contains a particular field name?
If you actually mean objects/classes, then there is "isprop" to check if a particular property exists.


Show 1 older comment
Walter Roberson
Walter Roberson on 11 Aug 2020
If you are working with class objects then removing any static property requires creating a new class. For class objects removing a property only makes sense for dynamic properties.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!