![]() Keen readers will have noticed that the mechanism that I outlined above for the Age property/method can also be used to add custom user methods. By setting the property’s GetMethod meta-property we avoid this need. Note that an alternative way to set the computed property Age would have been to set its value to be an anonymous function, but this would have necessitated invoking it with parenthesis (as in: ageInSecs = newFig.Age()). > ageString = datestr(ageInDays, 'HH:MM:SS.FFF') ![]() > ageDuration = duration(ageInDays*24,0,0) > ageString = datestr (ageInDays, 'HH:MM:SS.FFF' ) > ageDuration = duration (ageInDays* 24, 0, 0 ) However, a “cleaner” approach seems to be to create new read-only properties for the figure’s CreationTime and Age:įirst, create a small Matlab function as follows, that attaches the CreationTime property to a figure: The simple answer would be to use setappdata with the creation date whenever we create a figure. Secondly, I happened to run into a perfect usage example a short while ago that called for this solution: a StackExchange user asked whether it is possible to tell a GUI figure’s age, in other words the elapsed time since the figure was created. ![]() Well, first, because my experience has been that this little tidbit is fairly unknown by Matlab developers. Then again, I’d have less to blog about in that case…Īnyway, why am I rehashing old news that I have already reported a few years ago? So much frustration could have been solved if MathWorks would simply remove the Sealed class meta-property from its classes. ![]() In other words, the new properties will only be added to the handles that we specifically request, and not to any others.Īll this is important, because for some unexplained reason that escapes my understanding, MathWorks chose to seal its classes, thus preventing users to extend them with sub-classes that contain the new properties. The bottom line is that we can dynamically add custom properties in run-time to any HG object, without affecting any other object. What is still NOT documented, as far as I could tell, is that all of Matlab’s builtin handle graphics objects indirectly inherit the dynamicprops class, which allows this. As I wrote in that post, in HG2 (R2014b onward), we can use the fully-documented addprop function to add new custom properties (and methods) to such objects. While there was never a fully-documented way to do this, most users simply attached such properties as fields in the UserData property or the object’s ApplicationData property (accessible via the documented setappdata/ getappdata functions).Īn undocumented way to attach actual new user-defined properties to objects such as GUI handles or Java references has historically (in HG1, up to R2014a) been to use the undocumented schema.prop function, as I explained here. For various purposes, it is sometimes useful to attach custom user-defined properties to such objects. Matlab objects have numerous built-in properties (some of them publicly-accessible/documented and others not, but that’s a different story).
0 Comments
Leave a Reply. |