MATLAB examples
To use MATLAB features in your Apama or Apama Capital Markets Foundation application, you must create a MATLAB session. The following examples show how to create a MATLAB session and how to use it to set or get floating point scalar values, arrays or matrices. Each get or set request has an associated response that indicates whether the request successfully completed.
Creating a MATLAB session
The following example creates a MATLAB session. A boolean value indicates whether MATLAB should open a new session or re-use an existing session.
monitor MatlabExample2
{
// ***** Creating a MATLAB session:
com.apamax.matlab.MatlabManager matlabManager;
action onload() {
// Spawn to a new context:
spawn run() to context("New Context");
}
action run() {
// Running in a context other than main, open a MATLAB session:
matlabManager.openSession(
"Session1", "openSessionRequest", false, 6, sessionOpened);
}
action sessionOpened(
string sessionID, string messageID, boolean success, string error) {
if (success) {
log "Session Opened";
} else {
log "Session failed to open - " + sessionID + ", "
+ messageID + ", " + success.toString() + ", " + error;
}
}
Working with scalar values
The following example shows how to set a scalar value:
action putFloatExample() {
matlabManager.putFloat(
"Session1", "putFloatRequest", "x", 10.0, putFloatCallback);
}
action putFloatCallback(
string sessionID, string messageID, boolean success, string error) {
if (success) {
log "Put Float Succeeded";
} else {
log "Put Float Failed - " + sessionID + ", " + messageID + ", "
+ success.toString() + ", " + error;
}
}
The following example shows how to get a scalar value:
action getFloatExample() {
matlabManager.getFloat(
"Session1", "getFloatRequest", "x", getFloatCallback);
}
action getFloatCallback(string sessionID, string messageID, float value,
boolean success, string error) {
if (success) {
log "Get Float Succeeded - value = " + value.toString();
} else {
log "Get Float Failed - " + sessionID + ", " + messageID + ", "
+ success.toString() + ", " + error;
}
}
Working with arrays
To set an array:
action putFloatSequenceExample() {
sequence<float> y := [0.0, 1.0, 2.71828, 3.14159];
matlabManager.putFloatSequence("Session1", "putFloatSequenceRequest",
"y", y, putFloatSequenceCallback);
}
action putFloatSequenceCallback(
string sessionID, string messageID, boolean success, string error) {
if (success) {
log "Put Float Sequence Succeeded";
} else {
log "Put Float Sequence Failed - " + sessionID + ", "
+ messageID + ", " + success.toString() + ", " + error;
}
}
To get an array:
action getFloatSequenceExample() {
matlabManager.getFloatSequence(
"Session1", "getFloatSequenceRequest", "y", getFloatSequenceCallback);
}
action getFloatSequenceCallback(string sessionID, string messageID,
sequence<float> value, boolean success, string error) {
if (success) {
log "Get Float Sequence Succeeded - value = " + value.toString();
} else {
log "Get Float Sequence Failed - " + sessionID + ", "
+ messageID + ", " + success.toString() + ", " + error;
}
}
Working with matrices
To set a matrix:
action putFloatMatrixExample() {
sequence<sequence<float>> matrix := [];
sequence<float> row1 := [-2.1, 3.5];
sequence<float> row2 := [5.0, 1.0, 7.9, 17.0];
sequence<float> row3 := [-20.0, -90.0, 25.0];
matrix.append(row1);
matrix.append(row2);
matrix.append(row3);
matlabManager.putFloatMatrix("Session1", "putFloatMatrixRequest",
"m", matrix, putFloatMatrixCallback);
}
action putFloatMatrixCallback(
string sessionID, string messageID, boolean success, string error) {
if (success) {
log "Put Float Matrix Succeeded";
} else {
log "Put Float Matrix Failed - " + sessionID + ", "
+ messageID + ", " + success.toString() + ", " + error;
}
}
To get a matrix:
action getFloatMatrixExample() {
matlabManager.getFloatMatrix(
"Session1", "getFloatMatrixRequest", "m", getFloatMatrixCallback);
}
action getFloatMatrixCallback(string sessionID, string messageID,
sequence<sequence<float>> value, boolean success, string error) {
if (success) {
log "Get Float Matrix Succeeded - value = " + value.toString();
} else {
log "Get Float Matrix Failed - " + sessionID + ", "
+ messageID + ", " + success.toString() + ", " + error;
}
}
As well as setting MATLAB variables, applications may also send requests to the MATLAB plug-in to evaluate any appropriate MATLAB expressions using the evaluate() action.
The following example shows how to use the MATLAB plug-in to add two matrices and get the result:
action evaluateRequestExample() {
// First matrix:
sequence<sequence<float>> matrix1 := [];
sequence<float> m1row1 := [1.0,2.0,3.0];
sequence<float> m1row2 := [4.0,5.0,6.0];
sequence<float> m1row3 := [7.0,8.0,9.0];
matrix1.append(m1row1);
matrix1.append(m1row2);
matrix1.append(m1row3);
// The MATLAB manager also provides 'doesNothing*' callbacks that can
// process the returns silently if the response is not needed.
matlabManager.putFloatMatrix("Session1", "putFloatMatrixRequest1",
"matrix1", matrix1, matlabManager.doesNothingCallback);
// Second matrix:
sequence<sequence<float>> matrix2 := [];
sequence<float> m2row2 := [2.0,5.0,8.0];
sequence<float> m2row3 := [3.0,6.0,9.0];
matrix2.append(m2row1);
matrix2.append(m2row2);
matrix2.append(m2row3);
matlabManager.putFloatMatrix("Session1", "putFloatMatrixRequest1",
"matrix2", matrix2, matlabManager.doesNothingCallback);
// Use MATLAB to add the two matrices.
// The expected size of the string to be returned:
integer STANDARD_OUTPUT_SIZE := 256;
// Although use of the MATLAB plug-in is asynchronous, requests are
// queued. This guarantees that the two putFloatMatrix() actions
// have already been processed.
matlabManager.evaluate("Session1", "evaluateRequest",
"matrix3 = matrix1 + matrix2", STANDARD_OUTPUT_SIZE, evaluateCallback);
}
action evaluateCallback(
string sessionID, string messageID, string output,
sequence<string> outputLines, boolean success, string error) {
if (success) {
matlabManager.getFloatMatrix(
"Session1", "getMatrixRequest", "matrix3", getMatrix3Callback);
} else {
log "Evaluate Failed - " + sessionID + ", "
+ messageID + ", " + success.toString() + ", " + error;
}
}
action getMatrix3Callback(string sessionID, string messageID,
sequence<sequence<float>> value, boolean success, string error) {
if (success) {
log "Get Float Matrix Succeeded - value = " + value.toString();
} else {
log "Get Float Matrix Failed - " + sessionID + ", "
+ messageID + ", " + success.toString() + ", " + error;
}
}
}