我正在尝试使用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;