[python-win32] OLE COM in Python
kycbusiness at kycbusiness.com
kycbusiness at kycbusiness.com
Mon May 23 11:59:45 CEST 2011
Dear sir,
I am looking for a sample python script to declare VARIANTs
similar to one shown below which is for Perl. Your help will be very
much appreciated.
Sincerely,
Kin Cheung
My Perl program is:
#!"C:\xampp\perl\bin\perl.exe"
print "Content-type: text/html \n\n"; #HTTP HEADER
# http://localhost/cgi-bin/xampp_perl/Matlab_from_Perl_D.pl
$value4=4.0;
use Win32::OLE;
use Win32::OLE::Variant;
# Simple perl script to execute commands in MATLAB.
# The name Win32::OLE is misleading; this actually uses COM
# Use existing instance if MATLAB is already running.
eval {$ml = Win32::OLE->GetActiveObject('Matlab.Application')};
die "MATLAB not installed" if $@;
unless (defined $ml) {
$ml = Win32::OLE->new('Matlab.Application')
or die "Oops, cannot start MATLAB";
}
#Create two SAFE_ARRAYs
$mRealA = Variant(VT_ARRAY|VT_R8|VT_BYREF,2,2);
$mImagA = Variant(VT_ARRAY|VT_R8|VT_BYREF,2,2);
#Set the values
$mRealA->Put(0,0,1);
$mRealA->Put(0,1,2);
$mRealA->Put(1,0,3);
$mRealA->Put(1,1,$value4);
$ml->PutFullMatrix('A','base',$mRealA, $mImagA);
# Execute the function in MATLAB and retrieve the results in a Variant array.
$ml->Execute('[V,D]=eig(A)');
$mReal = Variant(VT_ARRAY|VT_R8|VT_BYREF,2,2);
$mImag = Variant(VT_ARRAY|VT_R8|VT_BYREF,2,2);
print "\n>> GetFullMatrix('V', 'base', ",'$mReal, $mImag',")\n"."<br />";
$ml->GetFullMatrix('V', 'base', $mReal, $mImag);
for ($i = 0; $i < 2; $i++) {
printf "%10.4f %10.4f\n"."<br />", $mReal->Get($i,0), $mReal->Get($i,1);
}
$mReal1 = Variant(VT_ARRAY|VT_R8|VT_BYREF,2,2);
$mImag1 = Variant(VT_ARRAY|VT_R8|VT_BYREF,2,2);
print "\n>> GetFullMatrix('D', 'base', ",'$mReal1, $mImag1',")\n"."<br />";
$ml->GetFullMatrix('D', 'base', $mReal1, $mImag1);
for ($i = 0; $i < 2; $i++) {
printf "%10.4f %10.4f\n"."<br />", $mReal1->Get($i,0), $mReal1->Get($i,1);
}
undef $ml; # close MATLAB if we opened it
More information about the python-win32
mailing list