| | |
Problem when passing arrays from c# to matlab
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
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:
This is my c# code (the try and catch statements have been removed here to make more readable):
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:
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!
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:
matlab Syntax (Toggle Plain Text)
function y = copy(a) y=a;
This is my c# code (the try and catch statements have been removed here to make more readable):
c# Syntax (Toggle Plain Text)
[DllImport(@"copy.dll")] private static extern bool copyInitialize(); [DllImport(@"copy.dll")] private static extern void copyTerminate(); [DllImport(@"copy.dll")] private static extern bool mlfCopy(int num, ref IntPtr output, [In]IntPtr input); [DllImport(@"mclmcrrt76.dll")] private static extern IntPtr mxCreateDoubleMatrix_700([In]Int32 numRows, [In]Int32 numCols, [In]Int32 mxComplexity); [DllImport(@"mclmcrrt76.dll")] private static extern IntPtr mxGetPr([In]IntPtr mxArray); [DllImport(@"mclmcrrt76.dll")] private static extern bool mclInitializeApplication(string options, int count); [DllImport(@"mclmcrrt76.dll")] private static extern void mclTerminateApplication(); static void Main(string[] args) { double[] copyInput = { 1.0}; mclInitializeApplication("NULL", 0); copyInitialize(); IntPtr inValA = mxCreateDoubleMatrix_700(copyInput.Length, 1, 0); int size = Marshal.SizeOf(copyInput [0]) * copyInput.Length; IntPtr outVal = Marshal.AllocHGlobal(size); // Copy the array to unmanaged memory. Marshal.Copy(copyInput , 0, inValA, copyInput .Length); //Run matlab function mlfCopy(1, ref outVal, inValA); // Get return value double[] copyAns = new double[copyInput .Length]; Marshal.Copy(outVal, copyAns, 0, copyInput.Length); copyTerminate(); 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:
c# Syntax (Toggle Plain Text)
[DllImport(@"copy.dll")] private static extern bool mlxCopy(int numOut, ref IntPtr output, int numIn, [In]IntPtr input); 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!
![]() |
Other Threads in the C# Forum
- Previous Thread: C# + SQL Login Form
- Next Thread: Is there a way to not to use a data base
| Thread Tools | Search this Thread |
.net access ado.net algorithm animation array barchart bitmap box broadcast buttons c# check checkbox client code color combobox control conversion csharp custom database datagrid datagridview dataset datastructure datetime degrees development draganddrop drawing encryption enum event excel file form format forms function gdi+ grade hash httpwebrequest image index input install java label lisp list listbox mandelbrot math mouseclick mp3 mysql operator path photoshop picturebox pixelinversion post print programming radians regex remote remoting richtextbox safari serialization server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser windows winforms wpf xml





