使用GetCharArray函数和PutCharArray函数可以在MATLAB与VBA之间传递字符串。[大谦MATLAB,dqmatlab点com]
1.GetCharArray函数
该函数从服务器程序获取字符串,调用格式为:
如果MATLAB用作客户程序:
string=h.GetCharArray('varname', 'workspace')
string=GetCharArray(h, 'varname', 'workspace')
string=invoke(h, 'GetCharArray', 'varname', 'workspace')
如果VBA用作客户程序:
void GetCharArray([in] BSTR varname, [in] BSTR workspace,
[out] BSTR string);
GetCharArray函数获取保存在变量varname中的字符串,varname变量位于句柄h表示的服务器的工作空间workspace中。获取的值返回到string中。workspace参数可以是base或global。
注意:如果试图将GetCharArray函数的输出显示到客户窗口上,必须指定一个输出变量,即string。
2.PutCharArray函数
使用该函数将字符串保存到服务器中,调用格式为:
如果MATLAB为客户程序:
h.PutCharArray('varname', 'workspace', 'string')
PutCharArray(h, 'varname', 'workspace', 'string')
invoke(h,'PutCharArray', 'varname', 'workspace', 'string')
如果VBA为客户程序:
void PutCharArray([in] BSTR name, [in] BSTR workspace,[in] BSTR string);
PutCharArray函数将服务器h的工作空间workspace中string参数内的字符串保存到变量varname中。workspace参数可以是base或global。
注意:string参数指定的字符串可以具有任意维数。
下面的例子用PutCharArray函数将字符串指定给服务器基本工作空间的变量str中。然后用GetCharArray函数读回到客户程序。
MATLAB用作客户程序时,使用下面的命令行:
>> h=actxserver('matlab.application');
>> h.PutCharArray('str', 'base', 'Hello MATLAB.');
>> S=h.GetCharArray('str', 'base')
S =
Hello MATLAB.
VBA用作客户程序时,使用类似下面的语句行:
Dim Matlab As Object
Dim S As text-blue-400">String
Set Matlab=text-blue-400">CreateObject("matlab.text-blue-400">application")
Call Matlab.PutCharArray("text-blue-400">str", "base"," Hello MATLAB.")
S=Matlab.GetCharArray("text-blue-400">str", "base")