OML Toolbox Method Examples

Toolbox methods include cell array, matrix, and struct.

Cell Array Example

The following function creates a cell array of the specified size and fills it sequentially starting with 0.
bool myfunc(OMLInterface* eval, const OMLCurrencyList* inputs, OMLCurrencyList* outputs)
{
OMLCurrency* size_cur = inputs->Get(0);
double       size     = size_cur->GetScalar();

OMLCellArray* out_cells = outputs->CreateCellArray(size, 1);

for (int j=0; j<size; j++)
{
    OMLCurrency* temp = outputs->CreateCurrencyFromDouble(j);
    out_cells->SetValue(j, temp);
}

outputs->AddCellArray(out_cells);

return true;
}

Matrix Example

bool myfunc(OMLInterface* eval, const OMLCurrencyList* inputs, OMLCurrencyList* outputs)
{
// make sure we have the proper number of inputs
if (inputs->Size() == 2)
{
OMLCurrency* input1 = inputs->Get(0);
OMLCurrency* input2 = inputs->Get(1);

// make sure both inputs are matrices
if (input1->IsMatrix() && input2->IsMatrix())
{
OMLMatrix* matrix1 = input1->GetMatrix();
OMLMatrix* matrix2 = input2->GetMatrix();

// make sure both matrices are the same size
if ((matrix1->GetRows() == matrix2->GetRows()) && (matrix1->GetCols() == matrix2->GetCols()))
{
int     size        = matrix1->GetRows()*matrix1->GetCols();

// make sure both matrices are real
if (matrix1->IsReal() && matrix2->IsReal())
{
double* mat1_data   = matrix1->GetRealData();
double* mat2_data   = matrix2->GetRealData();
// allocate space for the result data
double* result_data = new double [size];

for (int j=0; j<size; j++)
result_data[j] = mat1_data[j] + mat2_data[j];

// OML will manage the result_data memory from here on

OMLMatrix* result = outputs->CreateMatrix(matrix1->GetRows(), matrix1->GetCols(), result_data);

outputs->AddMatrix(result);
}
}
}
}
return true;
}

Struct Example

bool myfunc(OMLInterface* eval, const OMLCurrencyList* inputs, OMLCurrencyList* outputs)
{
// create a struct that holds all the US states and their capitals
// abbreviated to a list of two states for this example
// create an empty struct of the proper size

OMLStruct* struct = outputs->CreateStruct(2,1);

// set the field values for each struct

struct->SetValue(0, “state”, outputs->GetCurrencyFromString(“Michigan”));
struct->SetValue(0, “capital”, outputs->GetCurrencyFromString(“Lansing”));
struct->SetValue(1, “state”, outputs->GetCurrencyFromString(“Ohio”));
struct->SetValue(1, “capital”, outputs->GetCurrencyFromString(“Columbus”));

outputs->AddStruct(struct);
return true;

}