delphimatlabdelphi-xeole

Call MATLAB function with string parameters from Delphi XE using OLE


I'm trying to call a MATLAB function from Delphi XE using an OLE object. The function has two string parameters. When I try the MATLAB code in MATLAB (2008a) itself, it is all OK, but for some reason I cannot get the input parameters to MATLAB from Delphi XE. How do I achieve this?

As you can see in my code, I also tried setting variables in the workspace, which is an acceptable workaround for me.

function Matlab_DoIt(const aInput, aOutput: string): string;
var
    vMatlab, vInput: Variant;
begin
    vInput := aInput;
    vMatlab := CreateOleObject('matlab.application');
    vMatlab.visible := 1;
    vMatlab.Execute('cd c:\localdata\LSCT\Matlab');
    // vMatlab.Execute('input=' + aInput); // nothing happens
    // vMatlab.PutCharArray('input', 'base', aInput); // nothing happens
    // vMatlab.PutCharArray('input', 'base', vInput); // bad variable type error
    // vMatlab.PutCharArray('input', 'global', aInput); // nothing happens
    // vMatlab.PutWorkspaceData('input', 'base', aInput); // nothing happens
    // vMatlab.PutWorkspaceData('input', 'base', vInput); // bad variable type error
    // vMatlab.PutWorkspaceData('input', 'global', aInput); // nothing happens
    // vMatlab.Execute(Format('LSCT_tool_run(%s,%s)', [aInput, aOutput])); // nothing happens
    // vMatlab.Execute(Format('LSCT_tool_run(''%s'',''%s'')', [aInput, aOutput])); // nothing happens
    // vMatlab.Execute(Format('LSCT_tool_run("%s","%s")', [aInput, aOutput])); // nothing happens
    vMatlab.Execute('LSCT_tool_run'); // creates the file, but it is empty
end;

The MATLAB code writes a txt file containing the two parameters:

function LSCT_tool_run(input_path, output_path)
    diary ([c:\localdata\LSCT\Matlab\MyFile.txt]);
    diary on;
    % fprintf(input); Only when I try to set the input variable.
    fprintf(inpput_path);
    fprintf(output_path);
    diary off;

Solution

  • After consulting with Mathworks, it turns out this is not possible. Release 2012 will be usable again in this regard.