MATLAB: Do I get a segmentation violation when sending an array to MATLAB 7.10 (R2010a) from C#

MATLAB

When I send an array from C# to MATLAB (as shown in the following code snippet), I get a segmentation violation.
if (userpath == null)
userpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\matlab";
Type mlt = Type.GetTypeFromProgID("Matlab.Desktop.Application");//.Desktop//Application.Single
MLApp.MLApp ml = (MLApp.MLApp)Activator.CreateInstance(mlt);
ml.Execute("userpath = " + userpath);
ml.Execute("cd " + userpath);
ml.PutWorkspaceData(name, "base", data);
ml.Execute(string.Format("imagesc({0});colormap(gray);axis image", name));

Best Answer

MATLAB crashes because requests are sent to MATLAB before it has been initialized.
A possible workaround is to use “Matlab.Application.Single” or “Matlab.Application” PROGID instead of “Matlab.Desktop.Application” and use a non-desktop MATLAB COM server.
If it is necessary to have a full desktop, you can send the command via C# by using “Execute(“desktop”)”as shown in the following code snippet.
Type mlt = Type.GetTypeFromProgID("Matlab.Application");
MLApp.MLApp ml = (MLApp.MLApp)Activator.CreateInstance(mlt);
ml.Execute("desktop");