# A really basic test of sending data to/from MATLAB using the
# ActiveX interface
use Win32::OLE;
use Win32::OLE::Variant;
# 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', sub {$_[0]->Quit;})
or die "Oops, cannot start MATLAB";
}
############################
# Execute a command in ML
############################
# Create a surface plot
$evalString = 'surf(peaks)';
$ML->Execute($evalString);
# Put some data in ML using a string
$ML->Execute('A = magic(4);');
############################
# Put some data into ML
############################
# First create two SAFE_ARRAYs (Can only pass SAFE_ARRAY?)
my $MReal = Variant(VT_ARRAY|VT_R8, [1,2], 2);
my $MImag = Variant(VT_ARRAY|VT_R8, [1,2], 2);
# Set the values
$MReal->Put(1,0,1);
$MReal->Put(1,1,2);
$MReal->Put(2,0,3);
$MReal->Put(2,1,4);
# Put the matrix into MATLAB
$ML->PutFullMatrix('B', 'base', $MReal, $MImag);
# GetFullMatrix is similar but requires unpacking the data
# from MATLAB into the Variant array. The data is passed
# from MATLAB by reference.
# Make real and complex arrays for the data.
my $M2Real = Variant(VT_ARRAY|VT_R8|VT_BYREF, [1,2], 2);
my $M2Imag = Variant(VT_ARRAY|VT_R8|VT_BYREF, [1,2], 2);
# Retrieve the data from MATLAB
$ML->GetFullMatrix('B', 'base', $M2Real, $M2Imag);
# Attempt to get at the contents of returned data
$Element = $M2Real->Get(1,0); # Element in Row 1, Col 1
print("Element in Row 1, Col 1: $Element\n");
# Wait 15 seconds before closing MATLAB
sleep(15);
print('Done waiting...');
Best Answer