Serving the Quantitative Finance Community

 
User avatar
fox
Topic Author
Posts: 0
Joined: August 21st, 2002, 7:04 am

Display graphics from Matlab DLL

June 23rd, 2004, 3:44 pm

I have written a matlab propram which will display some plots. Then, I compiled this in MS visual studio as a DLL. But, wheneven I call this DLL to the point that it displays the graph, it aborts. I traced in debugger, and it aborted at the point of calling the plot function.For example, the original matlab m file is:function yy=plotGraph()yy=0;y=[1 2 3 4];plot(y);Appreciate if someone can give me a hint.
 
User avatar
fox
Topic Author
Posts: 0
Joined: August 21st, 2002, 7:04 am

Display graphics from Matlab DLL

June 23rd, 2004, 10:07 pm

Seems Matlab C/C++ graphics library is only officially supported to generate stand alone executable, not DLL. To compile into DLL, they recommend to use COM builder. But, I only have Excel builder installed. Can I use excel builder to compile a COM object and use it from C++ and how? Thanks.
 
User avatar
hmerkinger
Posts: 0
Joined: May 10th, 2004, 6:39 pm

Display graphics from Matlab DLL

June 24th, 2004, 10:06 am

fox,>Seems Matlab C/C++ graphics library is only officially supported to generate stand alone executable, not DLL. It is true that is not officially supported but it works nevertheless without any problems.>To compile into DLL, they recommend to use COM builder.You do not need it. All you need is the Matlab compiler.Here is a simple example:------------------function sinPlot;t = [-pi:0.1:+pi];plot(sin(t));return;------------------Compile this in Matlab to a DLL:>> mcc -t -L C -W libhg:sinPlotDll -T link:lib -h sinPlot.m libmmfile.mlibThis gives you the following output files:sinplot.hsinplot.csinplotdll.csinplotdll.hsinplotdll.exportssinplotdll.mlibsinPlotDll.dllsinPlotDll.libsinPlotDll.expNow construct a new C/C++ Win32 console project in MSVC.-------------#include "sinplotdll.h"#include "matlab.h"#include <stdio.h>void main(void){/* Call the library initialization function */sinPlotDllInitialize(); /* Call the implementation function */mlfSinplot();/* Call the library termination function */sinPlotDllTerminate();}----------The exact settings and imports you have to use can be foundon:http://www.mathworks.com/support/soluti ... =1-18CBIIt is crucial that you read this document carefully and really set all compiler-switchesetc. as described.A final note of caution: Check for case sensitivity. In the example above, sinPlotDllInitialize(); and sinPlotDllTerminate(); start with a lowercase "s" and mlfSinplot(); has to be written in exactly this way.To find out the exact spelling necessary look at sinplotdll.h, where you find functiondefinitions generated by Matlab compiler:extern void InitializeModule_sinplotdll(void);extern void TerminateModule_sinplotdll(void);extern void mlfSinplot(void);extern void mlxSinplot(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]);extern void sinPlotDllInitialize(void);extern void sinPlotDllTerminate(void);Finally, be sure to place sinPlotDll.dll in a directory on your system path or in the diretorywhere you execute your exe.I hope this solves your problem. If you have problems replicating this solution, I can sendyou the project files (mail: erkinger_at_gloriosia.com)Hans-Marc Erkingerwww.gloriosia.com - home of Trading with Matlab
 
User avatar
fox
Topic Author
Posts: 0
Joined: August 21st, 2002, 7:04 am

Display graphics from Matlab DLL

June 24th, 2004, 3:46 pm

hmerkinger,Thanks for your detailed explanation. I can get the graphic functions working in DLL. But, I still wonder if I can use the mcc command (as I don't have COM builder) to compile a COM object and integrate it with other client programs in MSVC. Thanks.
 
User avatar
hmerkinger
Posts: 0
Joined: May 10th, 2004, 6:39 pm

Display graphics from Matlab DLL

June 24th, 2004, 8:21 pm

fox,AFAIK the Matlab COM Builder is an extension to the Matlab Compiler.If you want to build COM objects, you need to have COM Builder.It would make no economic sense otherwise, since Matlab Compilercosts USD 5000 and COM Builder USD 3000. If it were so easy to buildCOM objects with the compiler alone, I think nobody would buy theCOM Builder. I would be interested if somebody knows how to buildCOM objects with the compiler alone, though.Regards,Hans-Marcwww.gloriosia.com - home of Trading with Matlab