Problem when passing arrays from c# to matlab

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 1
Reputation: Sarjo is an unknown quantity at this point 
Solved Threads: 0
Sarjo Sarjo is offline Offline
Newbie Poster

Problem when passing arrays from c# to matlab

 
0
  #1
Apr 11th, 2008
I am writing a c# application where I need to use matlab functions from my c# sharp code. From the beginning I used the Matlab Automation Server, which worked fine on my computer which has Matlab 7.4.0 (R2007a) installed. However, the application needs to be usable on computers with older versions of Matlab, and it turned out that the automation server can't be used then.

Now I am trying to solve this problem using Matlab Compiler to create dll files of my matlab functions and then call them from c#, based on these two code examples: http://www.mathworks.com/support/sol...a/1-X1PFC.html
http://www.mathworks.com/matlabcentr...bjectType=file

My problem has to do with passing arrays to and from my matlab functions. As long as I only use scalar values or arrays containing only one value, everything works fine, but as soon as the array contains more than one value an error occurs.

This is my code:
The copy.m matlab function only returns a copy of the input argument:
  1.  
  2. function y = copy(a)
  3. y=a;

This is my c# code (the try and catch statements have been removed here to make more readable):
  1. [DllImport(@"copy.dll")]
  2. private static extern bool copyInitialize();
  3. [DllImport(@"copy.dll")]
  4. private static extern void copyTerminate();
  5. [DllImport(@"copy.dll")]
  6. private static extern bool mlfCopy(int num, ref IntPtr output, [In]IntPtr input);
  7.  
  8. [DllImport(@"mclmcrrt76.dll")]
  9. private static extern IntPtr mxCreateDoubleMatrix_700([In]Int32 numRows, [In]Int32 numCols, [In]Int32 mxComplexity);
  10. [DllImport(@"mclmcrrt76.dll")]
  11. private static extern IntPtr mxGetPr([In]IntPtr mxArray);
  12. [DllImport(@"mclmcrrt76.dll")]
  13. private static extern bool mclInitializeApplication(string options, int count);
  14. [DllImport(@"mclmcrrt76.dll")]
  15. private static extern void mclTerminateApplication();
  16.  
  17. static void Main(string[] args)
  18. {
  19. double[] copyInput = { 1.0};
  20.  
  21. mclInitializeApplication("NULL", 0);
  22. copyInitialize();
  23.  
  24. IntPtr inValA = mxCreateDoubleMatrix_700(copyInput.Length, 1, 0);
  25.  
  26. int size = Marshal.SizeOf(copyInput [0]) * copyInput.Length;
  27.  
  28. IntPtr outVal = Marshal.AllocHGlobal(size);
  29.  
  30. // Copy the array to unmanaged memory.
  31. Marshal.Copy(copyInput , 0, inValA, copyInput .Length);
  32.  
  33. //Run matlab function
  34. mlfCopy(1, ref outVal, inValA);
  35.  
  36. // Get return value
  37. double[] copyAns = new double[copyInput .Length];
  38. Marshal.Copy(outVal, copyAns, 0, copyInput.Length);
  39.  
  40. copyTerminate();
  41. mclTerminateApplication();

When running this code the returned value is identical to the value in copyInput, however, as soon as the size of copyInput is changed (double[] copyInput = { 1.0, 2.0}; ) an error occurs (without any more information than a standard windows error message). I have also tried to use:
  1. [DllImport(@"copy.dll")]
  2. private static extern bool mlxCopy(int numOut, ref IntPtr output, int numIn, [In]IntPtr input);
  3. mlxCopy(1, ref outVal, 1, inValA);

Instead of the mlfCopy, but this doesn't work either.

Is there anyone who has an idea of how to solve this problem? What I really need is to be able to pass two-dimensional arrays to and from matlab functions.

Thanks in advance!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC