Main Content

How MATLAB Handles Undefined Methods

If your MATLAB® command invokes a nonexistent method on a Java® object, MATLAB looks for a function with the same name. If MATLAB finds a function of that name, it attempts to invoke it. If MATLAB does not find a function with that name, it displays a message stating that it cannot find a method by that name for the class.

For example, MATLAB has a function named size, and the Java API java.awt.Frame class also has a size method. If you call size on a Frame object, the size method defined by java.awt.Frame is executed. However, if you call size on an object of java.lang.String, MATLAB does not find a size method for this class. Therefore, it executes the MATLAB size function instead.

text = java.lang.String('hello'); 
ans =
     1     1


When you define a Java class for use in MATLAB, avoid giving any of its methods the same name as a MATLAB function.