Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: MATLAB and XE7



Permlink Replies: 5 - Last Post: Aug 27, 2015 11:01 AM Last Post By: Remy Lebeau (Te...
Boris Epel

Posts: 4
Registered: 9/27/02
MATLAB and XE7
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 26, 2015 2:19 PM
HI,

I was using an interface between c++Builder and MATLAB:
(http://www.mathworks.com/matlabcentral/newsreader/view_thread/310918)

Procedure execute("Execute");

Variant matlab = Variant::CreateObject( "Matlab.Application" );

// Execute a command in MATLAB
matlab.Exec(execute.ClearArgs() << "x=0:.03:1.5; figure; plot(x,sin(x))");

// Send an array to MATLAB
int Bounds[2] = {0,9};
Variant a = VarArrayCreate(Bounds, 1, varDouble);
for (unsigned i=0; i <= 9; i++)
{
double tmp = i*i;
a.PutElement(&tmp, i);
}
matlab.OleProcedure("PutWorkspaceData", "b", "base", a);

After upgrade to XE7 and MATLAB 2014, OLE interface started to come up with error
EOleSysError 'Bad variable type'

By adding AnsiString to the argument I fixed the 6th line:
matlab.Exec(execute.ClearArgs() << AnsiString("x=0:.03:1.5; figure; plot(x,sin(x))"));
but there is still an error in matlab.OleProcedure ...

So this looks like some very general string/array representation issue - may be somebody have an idea ?

Thanks! Boris
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: MATLAB and XE7
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 26, 2015 3:39 PM   in response to: Boris Epel in response to: Boris Epel
Boris wrote:

After upgrade to XE7 and MATLAB 2014, OLE interface started to come
up with error EOleSysError 'Bad variable type'

Try changing your string literals from Ansi literals to Unicode literals:

matlab.Exec(execute.ClearArgs() << L"x=0:.03:1.5; figure; plot(x,sin(x))");
...
matlab.OleProcedure(L"PutWorkspaceData", L"b", L"base", a);


Or at least wrap them in System::(Unicode)String objects:

matlab.Exec(execute.ClearArgs() << String("x=0:.03:1.5; figure; plot(x,sin(x))"));
...
matlab.OleProcedure(String("PutWorkspaceData"), String("b"), String("base"), 
a);


--
Remy Lebeau (TeamB)
Boris Epel

Posts: 4
Registered: 9/27/02
Re: MATLAB and XE7
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 27, 2015 10:06 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you Remy,

1.
matlab.Exec(execute.ClearArgs() << L"x=0:.03:1.5; figure; plot(x,sin(x))");
causing program to crash
matlab.Exec(execute.ClearArgs() << String("x=0:.03:1.5; figure; plot(x,sin(x)))");
does not compile due to ambiguity in << operator
as I mentioned
matlab.Exec(execute.ClearArgs() << "x=0:.03:1.5; figure; plot(x,sin(x))");
is causing program to crash

matlab.Exec(execute.ClearArgs() << AnsiString("x=0:.03:1.5; figure; plot(x,sin(x)))");
works fine

2.
Matlab.OleProcedure(L"PutWorkspaceData", L"bbb", L"base", a);
executes fine, but does not do anything
Matlab.OleProcedure( String("PutWorkspaceData"), String("bbb"), String("base"), a);
'Bad Variable type'

Matlab.OleProcedure( WideString("PutWorkspaceData"), WideString("bbb"), WideString("base"), a);
seems working

worth noting: XE7+MATLAB 2014+ x64 Windows7+32bit compiler
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: MATLAB and XE7
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 27, 2015 11:01 AM   in response to: Boris Epel in response to: Boris Epel
Boris wrote:

matlab.Exec(execute.ClearArgs() << L"x=0:.03:1.5; figure;
plot(x,sin(x))");
causing program to crash

Looking at sysvari.h, I see ClearArgs() returns an AutoCmd, and AutoCmd does
have an << operator for wchar_t* input. However, looking at variant.cpp,
I see that it just passes the value to a Variant (specifically, to Variant::PutElement()),
and Variant interprets a wchar_t* value as a COM BSTR, which this is not.
You would have to use the WideString class to address that:

matlab.Exec(execute.ClearArgs() << WideString("x=0:.03:1.5; figure; plot(x,sin(x))").c_bstr());


matlab.Exec(execute.ClearArgs() << String("x=0:.03:1.5; figure; plot(x,sin(x)))");
does not compile due to ambiguity in << operator
as I mentioned

You did not mention an ambiguity error earlier.

AutoCmd does not have an << operator for String input. It has one for Variant
(which can be constructed from a String), and one for AnsiString (which can
also be constructed from a String). So the compiler does not know which
one to contructor. So you have to be more explicit:

matlab.Exec(execute.ClearArgs() << Variant(String("x=0:.03:1.5; figure; plot(x,sin(x)))")));


Or:

matlab.Exec(execute.ClearArgs() << AnsiString("x=0:.03:1.5; figure; plot(x,sin(x)))"));


matlab.Exec(execute.ClearArgs() << "x=0:.03:1.5; figure;
plot(x,sin(x))");
is causing program to crash

AutoCmd does not have an << operator for char* input. So the compiler has
to be doing an inplicit conversion to either Variant or AnsiString. I'm
leaning towards Variant, because Variant has been known to have problems
with char* values. So best to convert to WideString or AnsiString instead,
as shown above.

Matlab.OleProcedure(L"PutWorkspaceData", L"bbb", L"base", a);
executes fine, but does not do anything

Internally, OleProcedure() assigns the 2nd+ parameters to a TAutoArgs array.
Depending on the order of includes between utilcls.h and System.hpp, TAutoArgs
holds either TVariantT<VARIANT> or System::Variant values. Both accept wchar_t*
input, but both expect wchar_t* to be BSTR. So WideString to the rescue
again:

Matlab.OleProcedure(L"PutWorkspaceData", WideString("bbb"), WideString("base"), 
a);


Matlab.OleProcedure( String("PutWorkspaceData"), String("bbb"),
String("base"), a);
'Bad Variable type'

That is odd, because TVariantT and Variant both support String input.

--
Remy Lebeau (TeamB)
david hoke

Posts: 616
Registered: 2/9/07
Re: MATLAB and XE7
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 27, 2015 10:12 AM   in response to: Boris Epel in response to: Boris Epel
<Boris Epel> wrote in message news:730180 at forums dot embarcadero dot com...
After upgrade to XE7 and MATLAB 2014,

For the record, (and possibly helpful),
what (versions) did you upgrade from?
Boris Epel

Posts: 4
Registered: 9/27/02
Re: MATLAB and XE7
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 27, 2015 10:17 AM   in response to: david hoke in response to: david hoke
Hi David,

XE4 -> XE7

however this may not be the compiler issue, but of MATLAB server as error came after reinstalling BOTH software (would be nice to know who really did it but I have this only system)

Boris

david hoke wrote:
<Boris Epel> wrote in message news:730180 at forums dot embarcadero dot com...
After upgrade to XE7 and MATLAB 2014,

For the record, (and possibly helpful),
what (versions) did you upgrade from?
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02