MATLAB Answers

can I store class properties in axes UserData?

9 views (last 30 days)
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.]

  1 Comment

Bruce Elliott
Bruce Elliott on 16 Apr 2020
Update: my attempts to resolve this have boiled my question down to a more basic one:
Why do plotting commands delete axes UserData?
I'll post this as a separate question.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 16 Apr 2020
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.

  1 Comment

Bruce Elliott
Bruce Elliott on 16 Apr 2020
Thanks, Steven - that's very helpful. I had not seen that since R2019b we can derive classes from matlab.graphics.chartcontainer.ChartContainer; that will change my project a lot.
My custom plot type is one that I've been working on as a back-burner project for several years, and now that it's nearing completion, I've seen that R2020a now includes this plot type in the Mapping Toolbox (!!).
It's for plotting 3-D data specified as latitude, longitude, altitude triplets without distortion. My group has used plot3(lon,lat,alt) for years as a "quick and dirty" way to do this, but for large distances the geometric distortion can be distracting or worse.
I wanted to create a custom axes class as well as custom line and patch classes that a user could use exactly as he would use the usual Cartesian plotting functions.
Now that the Mapping Toolbox has much improved 3-D plotting, most of what I've done will be obsolete before it's finished. Oh, well. It was fun, anyway.

Sign in to comment.

More Answers (1)

Bruce Elliott
Bruce Elliott on 16 Apr 2020
I think I've answered my own question (which happens all the time when I finally resort to posting a question here - see Rubber Ducky code debugging).
I just need to set 'hold on' (i.e. axes NextPlot property to 'add'), and my axes properties will persist.
I had not realized that if NextPlot is set to 'replace' all the axes properties are cleared, not just the Children objects.

  1 Comment

Bruce Elliott
Bruce Elliott on 20 Apr 2020
As Steven Lord pointed out in a reponse to the other thread on the general question, not all axes properties are reset to defaults, e.g. Units and Position persist.

Sign in to comment.


Translated by