I'm working on a command line application for ultrasound simulation in MATLAB. Nearly every object in our code is a subclass of handle (to pass as references). The problem I'm having is that all the methods inherited from the handle class shows up under the "Methods" section in MATLAB (see example below).
What I want is to hide the inherited methods from the handle class so that only the function the user is allowed to use is shown under "Methods". This way it doesn't look so messy for the user if he/she wants to know which methods to use.
Example Test class:
classdef Test < handle
methods
function myFunction(obj)
end
end
end
In the command line:
T = Test()
T =
Test handle with no properties.
Methods, Events, Superclasses
After clicking on "Methods":
Methods for class Test:
Test delete findobj ge isvalid lt ne
addlistener eq findp开发者_StackOverflow社区rop gt le myFunction notify
What I want:
Methods for class Test:
Test myFunction
Is this possible in MATLAB?
If you overload all of the subclass methods in a hidden methods block I think it will do exactly what you're looking for.
I'm not sure which versions of Matlab this works in, but it definitely works for me in R2012b.
The exception is isvalid
as it is Sealed so you can't override it in a handle subclass.
classdef handle_light < handle
methods(Hidden)
function lh = addlistener(varargin)
lh = addlistener@handle(varargin{:});
end
function notify(varargin)
notify@handle(varargin{:});
end
function delete(varargin)
delete@handle(varargin{:});
end
function Hmatch = findobj(varargin)
Hmatch = findobj@handle(varargin{:});
end
function p = findprop(varargin)
p = findprop@handle(varargin{:});
end
function TF = eq(varargin)
TF = eq@handle(varargin{:});
end
function TF = ne(varargin)
TF = ne@handle(varargin{:});
end
function TF = lt(varargin)
TF = lt@handle(varargin{:});
end
function TF = le(varargin)
TF = le@handle(varargin{:});
end
function TF = gt(varargin)
TF = gt@handle(varargin{:});
end
function TF = ge(varargin)
TF = ge@handle(varargin{:});
end
function TF = isvalid(varargin)
TF = isvalid@handle(varargin{:});
end
end
end
If you save the above class to handle_light.m and then type methods handle_light
in the command window you will get the following result:
Methods for class handle_light:
handle_light isvalid
The Test class then becomes:
classdef Test < handle_light
methods
function myFunction(obj)
end
end
end
Doing it in this way means that you don't need to put the overloads in the Test class which keeps things neater.
There is a solution here, including sample code.
In short, what you need to do is to overload Matlab's built-in function methods
, so that when it is called on your class, it removes the methods of handle
from the output. Make sure it works on everything else, though so that you don't mess up your user's other code. If you don't use the @foldername
variant to store your class, you could put it into a private
directory, for example.
Not a full solution, but if you do methods(T, '-full')
, then it at least tells you which methods are inherited from handle, so you know what to ignore.
Just get the functions from the inherited class and cancel them out with the ones from the main class using setdiff.
mH = methods('handle');
m = methods('MyClass');
m = setdiff(m,mH);
精彩评论