BarneySimpson has asked for the wisdom of the Perl Monks concerning the following question:

Dear perl monks I am haveing some trouble with Win32::OLE.

First I run Matlab and then the perl code. The command GetActiveObject('Matlab.Application') does not find the existing instance of matlab and so it creates its own with the new('Matlab.Application') command, This command runs Matlab commandline.

use Win32::OLE; # 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"; }

My question is why is it not finding the instance of matlab aready open. Thank you in advance

Replies are listed 'Best First'.
Re: OLE can't find Matlab aready running
by sanju7 (Acolyte) on Oct 04, 2010 at 21:00 UTC

    After looking at the code i can say it would only create new Matlab process if the earlier $ml getting "undef" in return. Also assuming a neat code (use my with new variable $ml use strict and warnings and also use win32::OLEs warning option to get more info on it etc)is causing the same --then the question comes to me how are you running the perl code. Unless you are running perl code from within Matlab how would perl be even aware of the existence of existing Matlab OLEs(correct me if i am wrong but its my understanding of OLEs). Unless you run Matlab from within perl like below,

    #!/usr/bin/perl # use strict; use warnings; use Win32::OLE; use Win32::OLE::Variant; Win32::OLE->Option(Warn => 3); my $Count = Win32::OLE->EnumAllObjects(sub { my $Object = shift; my $Class = Win32::OLE->QueryObjectType($Object); printf "# Object=%s Class=%s\n", $Object, $Class; }); print "Found $Count OLE Object(s)\n";

    If this generates any output then you can consider the approach you are trying. It would be easy by not bothering with OLE, import, say, a CSV file with Matlab import. I would probably start along that route: write a CSV file with the data you want in Perl, and then import it into matlab from the matlab macro.