Package matlabcontrol
Class LoggingMatlabProxy
- java.lang.Object
-
- matlabcontrol.MatlabProxy
-
- matlabcontrol.LoggingMatlabProxy
-
- All Implemented Interfaces:
MatlabOperations
public final class LoggingMatlabProxy extends MatlabProxy
Wraps around aMatlabProxyto provide a log of interactions. The data is not altered. This logger is useful for determining the Java types and structure of data returned from MATLAB.
Interaction with all methods, except those defined inObjectand not overridden, is logged. Entering a method, exiting a method, and throwing an exception are logged. Method parameters and return values are logged. The contents of a returned array will be recursively explored and its contents logged. As is convention, all of these interactions are logged atLevel.FINER. If the logging system has not been otherwise configured, then theConsoleHandlerwhich prints log messages to the console will not show these log messages as their level is too low. To configure theConsoleHandlerto show these log messages, callshowInConsoleHandler().
This class is unconditionally thread-safe.- Since:
- 4.1.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class matlabcontrol.MatlabProxy
MatlabProxy.DisconnectionListener, MatlabProxy.Identifier, MatlabProxy.MatlabThreadCallable<T>, MatlabProxy.MatlabThreadProxy
-
-
Constructor Summary
Constructors Constructor Description LoggingMatlabProxy(MatlabProxy delegateProxy)Constructs the logging proxy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddDisconnectionListener(MatlabProxy.DisconnectionListener listener)Adds a disconnection listener that will be notified when this proxy becomes disconnected from MATLAB.booleandisconnect()Disconnects the proxy from MATLAB.voideval(java.lang.String command)Evaluates a command in MATLAB.voidexit()Exits MATLAB.voidfeval(java.lang.String functionName, java.lang.Object... args)Calls a MATLAB function with the namefunctionName, returning the result.MatlabProxy.IdentifiergetIdentifier()Returns the unique identifier for this proxy.java.lang.ObjectgetVariable(java.lang.String variableName)Gets the value ofvariableNamein MATLAB.<U> UinvokeAndWait(MatlabProxy.MatlabThreadCallable<U> callable)Runs thecallableon MATLAB's main thread and waits for it to return its result.booleanisConnected()Whether this proxy is connected to MATLAB.booleanisExistingSession()Whether this proxy is connected to a session of MATLAB that was running previous to the request to create this proxy.booleanisRunningInsideMatlab()Whether this proxy is running inside of MATLAB.voidremoveDisconnectionListener(MatlabProxy.DisconnectionListener listener)Removes a disconnection listener.java.lang.Object[]returningEval(java.lang.String command, int nargout)Evaluates a command in MATLAB, returning the result.java.lang.Object[]returningFeval(java.lang.String functionName, int nargout, java.lang.Object... args)Calls a MATLAB function with the namefunctionName, returning the result.voidsetVariable(java.lang.String variableName, java.lang.Object value)SetsvariableNametovaluein MATLAB, creating the variable if it does not yet exist.static voidshowInConsoleHandler()Configures theConsoleHandlerresponsible for showing logging records to show the records that are logged by this proxy.java.lang.StringtoString()Returns a brief description of this proxy.
-
-
-
Constructor Detail
-
LoggingMatlabProxy
public LoggingMatlabProxy(MatlabProxy delegateProxy)
Constructs the logging proxy. All methods defined inMatlabProxywill be delegated todelegateProxy.- Parameters:
delegateProxy-
-
-
Method Detail
-
showInConsoleHandler
public static void showInConsoleHandler()
Configures theConsoleHandlerresponsible for showing logging records to show the records that are logged by this proxy. This behavior is useful if you have not otherwise configured logging in your application.
-
eval
public void eval(java.lang.String command) throws MatlabInvocationExceptionDescription copied from interface:MatlabOperationsEvaluates a command in MATLAB. This is equivalent to MATLAB'seval('command').- Parameters:
command- the command to be evaluated in MATLAB- Throws:
MatlabInvocationException
-
returningEval
public java.lang.Object[] returningEval(java.lang.String command, int nargout) throws MatlabInvocationExceptionDescription copied from interface:MatlabOperationsEvaluates a command in MATLAB, returning the result. This is equivalent to MATLAB'seval('command').
In order for the result of this command to be returned the number of arguments to be returned must be specified bynargout. This is equivalent in MATLAB to the number of variables placed on the left hand side of an expression. For example, in MATLAB theinmemfunction may be used with either 1, 2, or 3 return values each resulting in a different behavior:
The returnedM = inmem; [M, X] = inmem; [M, X, J] = inmem;Objectarray will be of lengthnargoutwith each return argument placed into the corresponding array position.
If the command cannot return the number of arguments specified bynargoutthen an exception will be thrown.- Parameters:
command- the command to be evaluated in MATLABnargout- the number of arguments that will be returned from evaluatingcommand- Returns:
- result of MATLAB command, the length of the array will be
nargout - Throws:
MatlabInvocationException
-
feval
public void feval(java.lang.String functionName, java.lang.Object... args) throws MatlabInvocationExceptionDescription copied from interface:MatlabOperationsCalls a MATLAB function with the namefunctionName, returning the result. Arguments to the function may be provided asargs, but are not required if the function needs no arguments.
The function arguments will be converted into MATLAB equivalents as appropriate. Importantly, this means that aStringwill be converted to a MATLABchararray, not a variable name.- Parameters:
functionName- the name of the MATLAB function to callargs- the arguments to the function- Throws:
MatlabInvocationException
-
returningFeval
public java.lang.Object[] returningFeval(java.lang.String functionName, int nargout, java.lang.Object... args) throws MatlabInvocationExceptionDescription copied from interface:MatlabOperationsCalls a MATLAB function with the namefunctionName, returning the result. Arguments to the function may be provided asargs, but are not required if the function needs no arguments.
The function arguments will be converted into MATLAB equivalents as appropriate. Importantly, this means that aStringwill be converted to a MATLABchararray, not a variable name.
In order for the result of this function to be returned the number of arguments to be returned must be specified bynargout. This is equivalent in MATLAB to the number of variables placed on the left hand side of an expression. For example, in MATLAB theinmemfunction may be used with either 1, 2, or 3 return values each resulting in a different behavior:
The returnedM = inmem; [M, X] = inmem; [M, X, J] = inmem;Objectarray will be of lengthnargoutwith each return argument placed into the corresponding array position.
If the function is not capable of returning the number of arguments specified bynargoutthen an exception will be thrown.- Parameters:
functionName- the name of the MATLAB function to callnargout- the number of arguments that will be returned byfunctionNameargs- the arguments to the function- Returns:
- result of MATLAB function, the length of the array will be
nargout - Throws:
MatlabInvocationException
-
setVariable
public void setVariable(java.lang.String variableName, java.lang.Object value) throws MatlabInvocationExceptionDescription copied from interface:MatlabOperationsSetsvariableNametovaluein MATLAB, creating the variable if it does not yet exist.- Throws:
MatlabInvocationException
-
getVariable
public java.lang.Object getVariable(java.lang.String variableName) throws MatlabInvocationExceptionDescription copied from interface:MatlabOperationsGets the value ofvariableNamein MATLAB.- Returns:
- value
- Throws:
MatlabInvocationException
-
invokeAndWait
public <U> U invokeAndWait(MatlabProxy.MatlabThreadCallable<U> callable) throws MatlabInvocationException
Description copied from class:MatlabProxyRuns thecallableon MATLAB's main thread and waits for it to return its result. This method allows for uninterrupted access to MATLAB's main thread between two or more interactions with MATLAB.
If running outside MATLAB thecallablemust beSerializable; it may not beRemote.- Specified by:
invokeAndWaitin classMatlabProxy- Returns:
- result of the callable
- Throws:
MatlabInvocationException
-
addDisconnectionListener
public void addDisconnectionListener(MatlabProxy.DisconnectionListener listener)
Description copied from class:MatlabProxyAdds a disconnection listener that will be notified when this proxy becomes disconnected from MATLAB.- Overrides:
addDisconnectionListenerin classMatlabProxy
-
removeDisconnectionListener
public void removeDisconnectionListener(MatlabProxy.DisconnectionListener listener)
Description copied from class:MatlabProxyRemoves a disconnection listener. It will no longer be notified.- Overrides:
removeDisconnectionListenerin classMatlabProxy
-
disconnect
public boolean disconnect()
Description copied from class:MatlabProxyDisconnects the proxy from MATLAB. MATLAB will not exit. After disconnecting, any method sent to MATLAB will throw an exception. A proxy cannot be reconnected. Returnstrueif the proxy is now disconnected,falseotherwise.- Specified by:
disconnectin classMatlabProxy- Returns:
- if disconnected
- See Also:
MatlabProxy.exit(),MatlabProxy.isConnected()
-
isExistingSession
public boolean isExistingSession()
Description copied from class:MatlabProxyWhether this proxy is connected to a session of MATLAB that was running previous to the request to create this proxy.- Overrides:
isExistingSessionin classMatlabProxy- Returns:
- if existing session
-
isRunningInsideMatlab
public boolean isRunningInsideMatlab()
Description copied from class:MatlabProxyWhether this proxy is running inside of MATLAB.- Specified by:
isRunningInsideMatlabin classMatlabProxy- Returns:
-
isConnected
public boolean isConnected()
Description copied from class:MatlabProxyWhether this proxy is connected to MATLAB.
The most likely reasons for this method to returnfalseare if the proxy has been disconnected viaMatlabProxy.disconnect()or MATLAB has been closed (when running outside MATLAB).- Specified by:
isConnectedin classMatlabProxy- Returns:
- if connected
- See Also:
MatlabProxy.disconnect(),MatlabProxy.exit()
-
getIdentifier
public MatlabProxy.Identifier getIdentifier()
Description copied from class:MatlabProxyReturns the unique identifier for this proxy.- Overrides:
getIdentifierin classMatlabProxy- Returns:
- identifier
-
exit
public void exit() throws MatlabInvocationExceptionDescription copied from class:MatlabProxyExits MATLAB. Attempting to exit MATLAB with either aevalorfevalcommand will cause MATLAB to hang indefinitely.- Specified by:
exitin classMatlabProxy- Throws:
MatlabInvocationException- See Also:
MatlabProxy.disconnect(),MatlabProxy.isConnected()
-
toString
public java.lang.String toString()
Description copied from class:MatlabProxyReturns a brief description of this proxy. The exact details of this representation are unspecified and are subject to change.- Overrides:
toStringin classMatlabProxy
-
-