MATLAB: Can I store class properties in axes UserData

handle_classuserdata

I've been developing a class for a specialized kind of axes. Since we cannot derive classes from the built-in axes class, I have created a handle class that has an associated axes object as one of its properties. Since a figure with one of these specialized axes will be saved in a .fig file rather than a .mat file, I am storing my class properties in the UserData property of the associated axes object. My class constructor will then look for these properties if it is passed a .fig file as in input argument.
Some of my class properties are customized graphics objects, which class methods will expect to see. My problem is that when I attempt to set the value of one of these properties, it deletes all the other UserData fields and replaces all of them with this single value.
Here is a simplified version of what is happening:
Before setting the property for the graphics object I have this:
myAxes.UserData.myClassProperties
where myClassProperties is a struct with a couple dozen fields for the class properties, one of which is called 'EarthPatchHandle'. When I attempt to set that by doing this:
myAxes.UserData.myClassProperties.EarthPatchHandle = surfl(...);
All the other fields of myClassProperties disappear, and the only remaining field is EarthPatchHandle.
I suspect that I'm getting into trouble by storing a handle class object as a property of another handle class in the UserData property of another handle class, but I'm not sure about that.
Does anyone have any idea what's wrong with this?
[Note: there might well be a better design for what I'm trying to do, such as storing my full class object in the axes UserData, rather than individual properties. I'll start another question if that seems likely.]

Best Answer

So you want to create a custom plot type, along the same idea as the spider plot? If you're using release R2019b or later, take a look at part 3 of that blog series. In part 3 Sean walks through creating a custom spider plot chart.
As for why the UserData of your axes gets cleared using your current approach, see this documentation page for an explanation. Two axes properties are not reset when trying to create a new plot in an axes with its NextPlot property set to 'replace': Units and Position. The EarthPatchHandle field of the struct array myClassProperties stored in UserData doesn't remain, but it is recreated after the UserData property was cleared at the start of the surfl call.