Main Content

JavaMethods That AffectMATLABCommands

MATLAB®commands that operate on Java®objects and arrays use the methods that are implemented within, or inherited by, the class. There are some MATLAB commands that you can alter in behavior by changing the Java methods that they use.

Changing the Effect ofdispanddisplay

You call thedispfunction when you:

  • Display the value of a variable or an expression in MATLAB.

  • Terminate a command line without a semicolon.

  • Display a Java object in MATLAB.

When callingdispon a Java object, MATLAB formats the output using the objecttoStringmethod. If the class does not implement this method, then MATLAB uses an inheritedtoStringmethod. If no intermediate ancestor classes define this method, MATLAB uses thetoStringmethod defined by thejava.lang.Objectclass.

To change the way MATLAB displays an object, implement your owntoStringmethod in your class definition.

Changing the Effect ofisequal

The MATLABisequalfunction compares two or more arrays for equality in type, size, and contents. Also, you can use this function to test Java objects for equality.

When you compare two Java objects usingisequal, MATLAB performs the comparison using the Java method,equals. MATLAB first determines the class of the objects specified in the command, and then uses theequalsmethod implemented by that class. Ifequalsis not implemented in this class, then MATLAB uses an inheritedequalsmethod. If no intermediate ancestor classes define this method, MATLAB uses theequalsmethod defined by thejava.lang.Objectclass.

To change the way MATLAB compares members of a class, implement your ownequalsmethod in your class definition.

Changing the Effect ofdouble,string, andchar

You can change the output of the MATLABdouble,string, andcharfunctions by defining your own Java methods,toDouble,toString, andtoChar. For more information, seeConvert to MATLAB Numeric TypesandConvert to MATLAB Strings.

Related Topics