How can I make Variable Editor show the value of a class object derived from "double"

10 visualizzazioni (ultimi 30 giorni)
I define a class, derived from 'double', like with MathWorks DocSimpleDouble example:
classdef DocSimpleDouble < double
methods
function obj = DocSimpleDouble(data)
if nargin == 0
data = 0;
end
obj = obj@double(data); % initialize the base class portion
end
end
end
I then create an object of this class, assign it some values:
>> x=DocSimpleDouble([11 12 13])
x =
DocSimpleDouble
double data:
11 12 13
Methods, Superclasses
So far so good, however when I open x in MATLAB's Variable Editor, it displays an empty array. I see no numerical values in the cells. Is there a way to make the Editor show the values, e.g., by adding a method to the class? I tried adding disp() but it did not help.
  1 Commento
Arnon
Arnon il 20 Ago 2012
No answer yet... hence I submitted it as a bug report: "Variable Editor does not show the content of a variable."

Accedi per commentare.

Risposte (3)

Malcolm Lidierth
Malcolm Lidierth il 7 Nov 2012
In answer to the OP: I have never tried subclassing a primitive data type. That I suspect is the source of the problem here and I am surprised it works at all. How are the superclass methods of double, which are MATLAB built-ins expecting IEEE doubles as input going to cope with a MATLAB object?
@Daniel's suggestion in the links he provides of overloading class is similar to something I have done in the "nakhur" class and its subclasses in the sigTOOL and the Project Waterloo File Utilities but does not seem to solve the above problem with built-ins. However, those classes do what the OP seems to be trying to do and work fine:
They provide a MATLAB OOP wrapper for primitive data - typically represented as a memmapfile object but also (within the same instance) as MATLAB arrays or GPU based arrays. To see all the tricks - see the code at http://sigtool.sourceforge.net/. Essentially:
subsref is overloaded to return the relevant data from the wrapped array
The size method is overloaded to report the size of the wrapped array.
The "isa" and some other methods are overloaded to report the result for the wrapped array (which is OK because of the overloading of subsref).
These objects cannot be passed to MATLAB built-ins, but they can be passed to most m-files, including those in the MATLAB toolboxes, where they behave as though they were the arrays passed back by subsref.
What's the point? Examples:
A wrapped memmapfile object behaves line a standard MATLAB matrix instead of needing special treatment - so works with the toolboxes.
If a large int16 array, e.g. from an ADC is wrapped, the object can wrap the 16 bit data but apply a scale and offset to return double 'real world unit' results via subsref.
sigTOOL has been using these methods since the mid-2000's - and no users have reported bugs related to the methods so far, so despite the apparent potential occasionally to trip up MATLAB, they seem to work.

Daniel Shub
Daniel Shub il 22 Ago 2012
I have essentially answered this question here and here.
The variable editor which makes use of proprietary aspects of com.mathworks.mlservices.MLArrayEditorServices. You could ask TMW to make this FOSS. You could also ask them for a product enhancement to improve the ability of the variable editor to handle custom classes (i.e., provide a hook).
An immediate and simple solution for what you have asked, is to overload the CLASS method to lie about the class of DocSimpleDouble. This efficentively fools com.mathworks.mlservices.MLArrayEditorServices. This is going to give you limited functionality since the variable editor is not going to know how to display any new properties that you add to your subclass. Also, getting your class methods to work with an overloaded CLASS method is going to be a real pain.
If you really want something like the variable editor, your best bet is to create a new method that generates a GUI and rebuild the variable editor from scratch. If you choose this road keep in mind the licensing restrictions on reverse engineering MATLAB.

Arnon
Arnon il 23 Ago 2012
Thank you Daniel for your detailed answer.
I added a 'class' method to my class, with return value 'dataset', and VE now displays the content. This solves my current needs. Interestingly, when I first tried to return 'double' VE did show it as type double but did not show the object's value (which is in fact a double! in my class I have no properties). Still 'dataset' works fine, it only uses a different display format.
So far, all my class methods work fine. I only had to change where I use class() in my code. I have multiple subclasses to this one, and use polymorphism. So I use isa() where possible (isa() is not fulled by overloading class()) and I added another method, realclass(), to each subclass which returns the real class name :) it's a stretch but it seems to do the work (and does not require digging into metaclass()).
One more quirk while overloading class(), I found that isa(x,'DocSimpleDouble') returns 1 isa(x,'double') is also 1, however, isa(x,'numeric') returns 0 (??) and yet, isnumeric(x) returns 1 (???).
So any one who is going to use this stuff, please be careful!
Thank you, Arnon

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by