Click here to Skip to main content
16,011,475 members
Home / Discussions / C#
   

C#

 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
N a v a n e e t h11-Apr-08 3:42
N a v a n e e t h11-Apr-08 3:42 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
Judah Gabriel Himango11-Apr-08 4:28
sponsorJudah Gabriel Himango11-Apr-08 4:28 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
N a v a n e e t h11-Apr-08 7:06
N a v a n e e t h11-Apr-08 7:06 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
Judah Gabriel Himango11-Apr-08 7:22
sponsorJudah Gabriel Himango11-Apr-08 7:22 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
Waleed Eissa11-Apr-08 5:10
Waleed Eissa11-Apr-08 5:10 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
Waleed Eissa11-Apr-08 5:00
Waleed Eissa11-Apr-08 5:00 
GeneralCustom C# .NET user control Pin
cloud strife11-Apr-08 3:21
cloud strife11-Apr-08 3:21 
QuestionProblem when passing arrays from c# to matlab Pin
sarjo11-Apr-08 3:08
sarjo11-Apr-08 3:08 
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/solutions/data/1-X1PFC.html
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=12987&objectType=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:
function y = copy(a)<br />
y=a;


This is my c# code (the try and catch statements have been removed here to make more readable):
[DllImport(@"copy.dll")]<br />
private static extern bool copyInitialize();<br />
[DllImport(@"copy.dll")]<br />
private static extern void copyTerminate();<br />
[DllImport(@"copy.dll")]<br />
private static extern bool mlfCopy(int num, ref IntPtr output, [In]IntPtr input);<br />
<br />
[DllImport(@"mclmcrrt76.dll")]<br />
private static extern IntPtr mxCreateDoubleMatrix_700([In]Int32 numRows, [In]Int32 numCols, [In]Int32 mxComplexity);<br />
[DllImport(@"mclmcrrt76.dll")]<br />
private static extern IntPtr mxGetPr([In]IntPtr mxArray);<br />
[DllImport(@"mclmcrrt76.dll")]<br />
private static extern bool mclInitializeApplication(string options, int count);<br />
[DllImport(@"mclmcrrt76.dll")]<br />
private static extern void mclTerminateApplication();<br />
<br />
static void Main(string[] args)<br />
{<br />
double[] copyInput = { 1.0};<br />
<br />
mclInitializeApplication("NULL", 0);<br />
copyInitialize();<br />
<br />
IntPtr inValA = mxCreateDoubleMatrix_700(copyInput.Length, 1, 0);<br />
<br />
int size = Marshal.SizeOf(copyInput [0]) * copyInput.Length;<br />
<br />
IntPtr outVal = Marshal.AllocHGlobal(size);<br />
<br />
// Copy the array to unmanaged memory.<br />
Marshal.Copy(copyInput , 0, inValA, copyInput .Length);<br />
<br />
//Run matlab function<br />
mlfCopy(1, ref outVal, inValA);<br />
<br />
// Get return value<br />
double[] copyAns = new double[copyInput .Length];<br />
Marshal.Copy(outVal, copyAns, 0, copyInput.Length);<br />
<br />
copyTerminate();<br />
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
[DllImport(@"copy.dll")]<br />
private static extern bool mlxCopy(int numOut, ref IntPtr output, int numIn, [In]IntPtr input);

and
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!
GeneralHelp me Pin
prachi1411-Apr-08 3:00
prachi1411-Apr-08 3:00 
GeneralRe: Help me Pin
carbon_golem11-Apr-08 3:04
carbon_golem11-Apr-08 3:04 
GeneralRe: Help me Pin
Pete O'Hanlon11-Apr-08 3:04
mvePete O'Hanlon11-Apr-08 3:04 
GeneralRe: Help me Pin
prachi1422-Apr-08 0:39
prachi1422-Apr-08 0:39 
GeneralRe: Help me Pin
Roger Alsing11-Apr-08 3:08
Roger Alsing11-Apr-08 3:08 
GeneralRe: Help me Pin
CPallini11-Apr-08 4:09
mveCPallini11-Apr-08 4:09 
GeneralRe: Help me Pin
Ashfield11-Apr-08 4:44
Ashfield11-Apr-08 4:44 
GeneralRe: Help me Pin
Abhijit Jana11-Apr-08 6:12
professionalAbhijit Jana11-Apr-08 6:12 
GeneralRadioButtonList Pin
Burim Rama11-Apr-08 2:40
Burim Rama11-Apr-08 2:40 
GeneralDispose for custom components Pin
N a v a n e e t h11-Apr-08 2:36
N a v a n e e t h11-Apr-08 2:36 
GeneralRe: Dispose for custom components Pin
carbon_golem11-Apr-08 3:06
carbon_golem11-Apr-08 3:06 
GeneralRe: Dispose for custom components Pin
N a v a n e e t h11-Apr-08 3:25
N a v a n e e t h11-Apr-08 3:25 
GeneralRe: Dispose for custom components Pin
carbon_golem11-Apr-08 3:39
carbon_golem11-Apr-08 3:39 
GeneralRe: Dispose for custom components Pin
N a v a n e e t h11-Apr-08 3:47
N a v a n e e t h11-Apr-08 3:47 
GeneralRe: Dispose for custom components Pin
Mike Dimmick11-Apr-08 4:01
Mike Dimmick11-Apr-08 4:01 
GeneralRe: Dispose for custom components Pin
N a v a n e e t h11-Apr-08 7:07
N a v a n e e t h11-Apr-08 7:07 
Generalbeforenavigate2 postdata to string Pin
parrimin11-Apr-08 2:02
parrimin11-Apr-08 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.