使用 OLE 从 Delphi XE 调用带字符串参数的 MATLAB 函数

问题描述 投票:0回答:1

我正在尝试使用 OLE 对象从 Delphi XE 调用 MATLAB 函数。该函数有两个字符串参数。当我在 MATLAB (2008a) 本身中尝试 MATLAB 代码时,一切正常,但由于某种原因,我无法从 Delphi XE 获取 MATLAB 的输入参数。我如何实现这一目标?

正如您在我的代码中看到的,我还尝试在工作区中设置变量,这对我来说是一个可以接受的解决方法。

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;

MATLAB 代码写入一个包含两个参数的 txt 文件:

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;
delphi matlab delphi-xe ole
1个回答
0
投票

咨询Mathworks后,发现这是不可能的。 2012 版将在这方面再次可用。

© www.soinside.com 2019 - 2024. All rights reserved.