matlabtest.g

matlabtest.g — test a connection to Matlab.

Code

[Note]

The code for this and other example scripts can be found in the DataHub distribution archive, typically at one of these locations:

    C:\Program Files\Cogent\OPC DataHub\scripts\

    C:\Program Files\Cogent\Cascade DataHub\scripts\

Please refer to Section 3.1, “How to Run a Script” for more information on using scripts.

require ("Application");
require ("MatlabSupport");

class MatlabTest Application
{
    engine;
}

method MatlabTest.destructor ()
{
    if (.engine)
        destroy (.engine);
}

/* This function does several reads and writes to MATLAB to ensure that
   we can translate back and forth between Gamma and MATLAB formats. */
method MatlabTest.send (str, var)
{
    local	gval1, gval3, errmsg;

    // For speed testing, squelch all princ statements
    //local	princ = listq;
	
    // Evaluate incoming string.  It sets var in the MATLAB instance
    if (str)
    {
        .engine.EvalString (str);
        errmsg = .engine.GetError();
    }
    // Read var from MATLAB.  Get its real part.
    gval1 = .engine.GetVariable(var).GetPr();
    // Write back to var with the result from MATLAB
    .engine.PutVariable (var, gval1);
    // Read it again, but this time write directly from the mxArray
    // instead of translating to Gamma form first.
    .engine.PutVariable (var, .engine.GetVariable(var));
    // Read it again and print it.  It should make sense.
    gval3 = .engine.GetVariable(var).GetPr();
	
    if (str)
        princ ("The statement: ", str, "\n");
    else
        princ ("The variable: ", var, "\n");
    if (errmsg)
        princ ("  produced: ", errmsg, "\n");
    else
        princ ("  produced: ", var, " = ", stringc(gval3), " with dimensions ", 
               .engine.GetVariable(var).GetDimensions(), "\n");
}

method MatlabTest.constructor ()
{
    .engine = engOpen("");
    if (!.engine)
        error ("Could not open MATLAB engine");
		
    .engine.SetVisible (1);

    // Simple numeric
    .send ("x = 5;", "x");

    // Simple string
    .send ("z = 'hello';", "z");

    // One-dimensional numeric array
    .send ("y = [1 2 3];", "y");

    // Two-dimension numeric arrays
    .send ("w = [1 2 3; 4 5 6; 7 8 9];", "w");
    .send ("w = [1 2; 4 5; 7 8];", "w");
	
    // MATLAB requires all strings to be the same length when evaluating
    // a MATLAB immediate array
    .send ("v = ['hello';'there';'folks']", "v");
    .send ("v = ['hello';'there';'guys']", "v");
	
    // But it handles variable length strings when taking an array
    // from Gamma.
    .engine.PutVariable ("u", ["this","is","a","test"]);
    .send (nil, "u");
	
    // We cannot handle matrices of strings, though.  The string length
    // is one of the two supported dimensions.
    try
    {
        .engine.PutVariable ("s", [["this","is"],["a","test"]]);
        .send (nil, "s");
    }
    catch
    {
        princ ("Expected error setting matrix of strings: ", _last_error_, "\n");
    }
	
    // Map variable x in MATLAB to variable xt in Gamma, updating once per second
    .engine.AutomaticVariable ("x", #xt, 1);
	
    // Create a DataHub point for z, then map MATLAB's z to it and
    // poll MATLAB for changes 5 times per second.
    datahub_command ("(create default:z)", 1);
    .engine.AutomaticVariable ("z", #$default:z, 0.2);
}

ApplicationSingleton (MatlabTest);