How do you get help for a class method/function when there are other functions of the same name?
For example, the predict method/function works for both TreeBagger and GeneralizedLinearModel class.
How do I get the respective helpfiles for these functions using doc, as currently doc predict returns the help for the CompactClassificationTree class?
http://www.mathworks.com/help/stats/treebagger.predict.html
http://www.mathworks.com/help/stats/generalizedlinearmodel.predict.html
predict is a method of each class. You can also call it like myTreeBagger.predict(data)
Related
I'm working on rewriting an old MATLAB package. As part of that rewrite I'm updating two classes (lets call them old_class1 and old_class2) defined using the class() function to classes defined with classdef.
The definition of old_class1 contains the line superiorto('old_class2'). This function is not allowed in a constructor defined with classdef, but I am unable to find any equivalent. How do I create functionality equivalent to superiorto() in a class defined with classdef?
superiorto is no longer available in MATLAB, but I was able to find a copy of old documentation that indicates it was used to determine which object's method was called when multiple different classes were given as function arguments. For example, given obj1=class1() and obj2=class2(), with the class1 constructor containing superiorto('class2'), would cause function(obj1,obj2) to callclass1.function, rather thanclass2.function`.
With modern classdef functionality, the same can be accomplished by setting the InferiorClasses property:
classdef (InferiorClasses = {?class2}) class1
Is there a quick way to count the number of methods in a MATLAB class ?
obj = myClassName()
Is there a way to get the number of methods inside this class?
Yes! At least in MatlabR2014b you can use methods
example:
methods('SURFPoints')
or
methods('myClassName')
Probably you would be able to find it with a quick google but you mix your terminology a bit. myClassName() is a class, and all the functions that are specific of this class and are "inside" it are called methods. Do not call method to a class! There is nothing like "number of functions used in a method" (well, there is but its definitely not what you are looking for).
You can use:
a = ?MyClassName;
numMethods = numel(a.MethodList);
Here a is a metaclass object that contains all the details of the class MyClassName, such as its package, properties, methods, events etc.
I want to add a method to the Graph class of the JUNG library using Eclipse. How would I do this?
I have the JUNG working correctly as a reference library by following this answer: https://stackoverflow.com/a/5618076/1949665
1) You have access to the source:
Simply add your method
2) You could extend the class and add the method in our extending class
3) Write a Util class with a static method implementing your method than simply uses the original class.
Your comment above implies that you want to be able to find cliques in a graph. (I didn't see the original question before you edited it.)
If so, it doesn't need to be a method on Graph itself, it just needs to accept a Graph as an argument. Graph is a type like List or Map, it should not have a method for every kind of algorithm you might want to use on a graph.
Tried many ways based on other questions asked here but could not get a method call to another class to compile.
I'm writing a dedicated class to process arithmetic, for example class Arithmetic. It has no class variables. It subclasses no class (though I tried NSObject with failure). It will have methods of the type:
+ (NSString *)calculateEnergy:(NSString *)weight;
that I want to call from another class, in a static-method way, like:
self.label.text=[arithmetic calculateEnergy:#"8.0"];
where 'Arithmetic *arithmetic' is declared and given a retain property (synthesised etc.) in the header and implementation of the class wanting to access the calculateEnergy method.
I've tried various suggestions, such as variations on:
[(Arithmetic *) arithmetic calculateEnergy:#"8.0"];
every time getting an "Accessing unknown 'calculateEnergy' getter method"
Thank you so much in advance!
Though I don't have the complete picture, your method definition is for a class-level, static method, as opposed to an instance method. You would want to call it as
self.label.text = [Arithmetic calculateEnergy:#"8.0"];
You also would not need to create an instance of Arithmetic to call the method you have there.
Is arithmetic not an instance of Arithmetic? If instead you try self.label.text=[Arithmetic calculateEnergy:#"8.0"]; you should be OK.
Method __call is running when come call to undefined method. I need to trigger when calling any existing method in class, something like __callAll.
The simpliest approach would be calling the method in every method, but I don't like this approach. The uses Zend framework.
Please advise me how to do it?
What you want isn't possible.
The only solution I can think of is the following:
Name all your functions, instead of myfunction, _myfunction.
Implement a __call method that wraps each of the function calls of the class to _myfunction.
Before executing the function, run your own code.
There's simply no other way to do this. I also wouldn't recommend using my way either but rather looking into some good code-design books because I bet what you want to do isn't necessary if you'd just have the right class design approach.