Click here to Skip to main content
15,895,011 members
Home / Discussions / C#
   

C#

 
GeneralRe: JPEG Decoder\Encoder in C# Pin
Gilad Kapelushnik12-Aug-09 5:01
Gilad Kapelushnik12-Aug-09 5:01 
AnswerRe: JPEG Decoder\Encoder in C# Pin
Dave Kreskowiak12-Aug-09 4:57
mveDave Kreskowiak12-Aug-09 4:57 
GeneralRe: JPEG Decoder\Encoder in C# Pin
Gilad Kapelushnik12-Aug-09 5:04
Gilad Kapelushnik12-Aug-09 5:04 
GeneralRe: JPEG Decoder\Encoder in C# Pin
Dave Kreskowiak12-Aug-09 6:25
mveDave Kreskowiak12-Aug-09 6:25 
QuestionInterfacing managed C# code and a C-shared Matlab library Pin
Jan Arjen Kraak12-Aug-09 4:44
Jan Arjen Kraak12-Aug-09 4:44 
AnswerRe: Interfacing managed C# code and a C-shared Matlab library Pin
Luc Pattyn12-Aug-09 7:46
sitebuilderLuc Pattyn12-Aug-09 7:46 
QuestionRe: Interfacing managed C# code and a C-shared Matlab library Pin
Cracked-Down6-Sep-09 23:21
Cracked-Down6-Sep-09 23:21 
AnswerRe: Interfacing managed C# code and a C-shared Matlab library Pin
Jan Arjen Kraak7-Sep-09 4:01
Jan Arjen Kraak7-Sep-09 4:01 
Hi Save-Tigers,

I managed to get it to work, thanks to the support of Matlab
although they mentioned several times I should buy the .net compiler
from them..

This is (part of my) code:

1) initialize matlab environment:
[DllImport("mclmcrrt710.dll", EntryPoint = "mclInitializeApplication_proxy")]
static extern bool InitiateMatlabApplication(string options, Int32 count);

call it from your code:
bool result = InitiateMatlabApplication(null, 0);

2) initialize your matlab code:
[DllImport("MatlabModel.dll", EntryPoint = "MatlabModelInitialize")]
static extern bool InitiateMatlabModel();

Note that MatlabModel must be replaced by whatever you named it
call it from your code:
bool result = InitiateMatlabModel();

3) Passing string arguments to Matlab:
[DllImport("MatlabModel.dll", EntryPoint = "mlfInitializeProject")]
static extern bool InitializeProject(IntPtr RootDir,
                                     IntPtr Name);

Where RootDir and Name are strings. Use mxcreate_string_proxy from Matlab
to convert to matlab-format:
[DllImport("mclmcrrt710.dll", EntryPoint = "mxCreateString_proxy", CharSet = CharSet.Ansi)]
static extern IntPtr CreateString([In][MarshalAs(UnmanagedType.LPStr)] string myString);

Use this in your code:
IntPtr ptrLocation = CreateString(strProjectLocation);
IntPtr ptrName = CreateString(Settings.ProjectName);

Now, use the pointers above to pass strings to matlab:
result = InitializeProject(ptrLocation, ptrName);

4) Retrieving double argument from Matlab:
I've created a function which returns a double, nothing more:
[DllImport("MatlabModel.dll", EntryPoint = "mlfGetNofSteps")]

To use it in your code:
IntPtr result_ptr = IntPtr.Zero;
bool result = GetNofSteps(1, ref result_ptr)

To convert the pointer to an integer:
[DllImport("mclmcrrt710.dll", EntryPoint = "mxGetScalar_proxy")]
static extern double GetScalar(IntPtr mxArray);

So, to convert the pointer to value:
int NumberOfSteps = Convert.ToInt32(GetScalar(result_ptr));

5) Retrieving from and sending arguments to Matlab:
I've created a function which returns some matrices and uses a double as variable:
[DllImport("MatlabModel.dll", EntryPoint = "mlfmyFunction")]
static extern bool myFunction(int nargout,
ref IntPtr ptrResultMatrix1,
ref IntPtr ptrResultMatrix2,
ref IntPtr ptrResultMatrix3,
IntPtr ptrSTep);

First, to create pointers to matrices Matlab can use as output parameters, use this:
[DllImport("mclmcrrt710.dll", EntryPoint = "mxCreateDoubleMatrix_700_proxy")]
static extern IntPtr CreateDoubleMatrix(int cols, int rows, int flags);

Use the above function to declare pointers to matrices in which Matlab will return the values:
// Create output BergingEindProc
IntPtr ptrResultMatrix1 = CreateDoubleMatrix(10, 1, 0);
IntPtr ptrResultMatrix2 = CreateDoubleMatrix(10, 1, 0);
IntPtr ptrResultMatrix3 = CreateDoubleMatrix(10, 1, 0);

Next, create matrix to use as input parameter:
// Create input ptrStapDag
double[] value = new double[1];
value[0] = Convert.ToDouble(DayStep);
IntPtr ptrStapDag = CreateDoubleMatrix(1, 1, 0);
IntPtr value_ptr = GetPtr(ptrStep);
Marshal.Copy(value, 0, value_ptr, 1);

Now the pointers are set, call the created function:
bool result = myFunction(3, ref ptrResultMatrix1, ref ptrResultMatrix2,
ref ptrResultMatrix3, ptrStep);

Notice the 'ref' for output parameters!

I hope the above sample code helps you out.
Maybe I should write an article on codeproject, explaining it in detail.

-- Stupidity should be painfull --

GeneralRe: Interfacing managed C# code and a C-shared Matlab library Pin
Cracked-Down7-Sep-09 4:17
Cracked-Down7-Sep-09 4:17 
GeneralRe: Interfacing managed C# code and a C-shared Matlab library Pin
nizar3213-Nov-09 11:24
nizar3213-Nov-09 11:24 
GeneralRe: Interfacing managed C# code and a C-shared Matlab library Pin
Jan Arjen Kraak19-Nov-09 20:43
Jan Arjen Kraak19-Nov-09 20:43 
GeneralRe: Interfacing managed C# code and a C-shared Matlab library2 Pin
nizar3213-Nov-09 11:24
nizar3213-Nov-09 11:24 
GeneralRe: Interfacing managed C# code and a C-shared Matlab library Pin
nizar3213-Nov-09 11:49
nizar3213-Nov-09 11:49 
GeneralRe: Interfacing managed C# code and a C-shared Matlab library Pin
Jan Arjen Kraak19-Nov-09 20:45
Jan Arjen Kraak19-Nov-09 20:45 
QuestionCode coverage Pin
K.v.S.12-Aug-09 4:20
K.v.S.12-Aug-09 4:20 
AnswerRe: Code coverage Pin
Luc Pattyn12-Aug-09 7:50
sitebuilderLuc Pattyn12-Aug-09 7:50 
QuestionQuerystring needed to set to empty Pin
kKamel12-Aug-09 4:08
kKamel12-Aug-09 4:08 
GeneralRe: Querystring needed to set to empty Pin
musefan12-Aug-09 4:54
musefan12-Aug-09 4:54 
GeneralRe: Querystring needed to set to empty Pin
kKamel12-Aug-09 5:04
kKamel12-Aug-09 5:04 
GeneralRe: Querystring needed to set to empty Pin
musefan12-Aug-09 5:07
musefan12-Aug-09 5:07 
AnswerRe: Querystring needed to set to empty Pin
Coding C#12-Aug-09 21:35
Coding C#12-Aug-09 21:35 
QuestionOpen Child Form and threading Pin
Saamir12-Aug-09 3:50
Saamir12-Aug-09 3:50 
AnswerRe: Open Child Form and threading Pin
0x3c012-Aug-09 3:57
0x3c012-Aug-09 3:57 
GeneralRe: Open Child Form and threading Pin
Saamir12-Aug-09 4:05
Saamir12-Aug-09 4:05 
GeneralRe: Open Child Form and threading Pin
0x3c012-Aug-09 4:16
0x3c012-Aug-09 4:16 

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.