I have a script in MATLAB that writes a CSV, the CSV is read by a c# script which writes a few more CSVs that I go back and read in MATLAB.
Is there any way to automate this so I don't have to call the c# code by hand each time?
It's very easy to call into .net from Matlab. The official documentation is at http://www.mathworks.co.uk/help/matlab/matlab_external/load-a-global-net-assembly.html You should be aware that Matlab is case-sensitive (even when it comes to specifying the assembly path) and that it is also limited in the kinds of objects it can pass back and forth across the boundary.
If you pass an array into your C# dll from Matlab, it will appear to be an array of bare objects rather than an array of numbers. In Matlab, you may need to use the char and cell methods to convert strings and arrays back into the form you are expecting.
To answer the title question, e.g. "Is it possible to call C# functions from MATLAB": yes, it is. Mathworks provides decent documentation on calling .NET assemblies from MATLAB on their website. Of course, there are limitations and some awkward quirks to take into account but basically you can create instances of .NET classes and interact with .NET applications from MATLAB.
To advise on automating this process, you could perhaps dive into the MATLAB COM Automation Service?
In the extension of this: it's also possible to call MATLAB functions in a .NET application. The other way around, sort of speak. This will be no problem with basic data types, but when it gets a bit more advances it can put you through some gnarly COM challenges, though.
Related
I want to apply 2D-DCT on the array of data collected from image pixels.
tried Accord.Math which is too slow to work with and also values vary from 50 to 100 as compared to matlab generated values which i think does not sounds good because i also have to apply LSB to these values.
MATLAB was applying DCT pretty fast so i want to use its dct2() method..
I have read some posts but mostly are about deploying m file as dll in C#..now the problem is dct2() depends on dct() which in turn depends in FFT()..and other checking functions too..
Now what should i use??? Deployment looks better option but how to include all dependencies??
and if any other suggestion please a little help or links so i can implement easily..actually I'm new in C# and also languages interfacing thing.
I think that you should use
deploytool and create an external library for your .m files ans use this in C#, I have linux so I do not have dotnet available, I have for java and C/C++ ,also you can investigate mcc, that compiles matlab code to create a dll like here enter image description here
I have a DLL which takes care of custom drawing for some special glass effects. I'm putting it in a DLL for three reasons: 1) So it can be easily re-used and distributed of course without weighing its host app down, 2) So I can distribute it to developers without them knowing how it works, and 3) So it can be used from C#. It currently works in Delphi, but I know I will need to do many changes to make it support C#. For example, the main DLL function includes 1 parameter (a Record) which contains a number of types I know won't work in C# (like String, and maybe TColor). Project isn't quite 100% done yet, but is working.
I need someone to point out the easiest approach to accomplishing this. The code is too large to post it all here, so here it is at Pastebin.
Here's what I need to know:
Should I keep using Records as I am, or use something else like Packed Record?
Any tricks to use something other than String or PChar in these Records?
How would I wrap this DLL in C#? (I know very little C# by the way)
How to define equivalent records to pass to DLL function?
How to define equivalent constants in C#? (C# version of JDGlassCommon.pas)
How to get canvas handle (HDC) and parent handle (HWND) to send to DLL?
What would be equivalent to TColor?
Is it safe to pass types such as TColor in the Records?
Do you foresee any other issues in my code?
File List:
Library: JDGlassLib.dll *
Unit: JDGlassCommon.pas *
Package: JDLib.bpl
Unit: JDGlassCommon.pas *
Unit: JDGlass.pas *
Program: JDLibTestApplicationD7.exe
Form: JDLibTestAppD7.dfm *
Unit: JDLibTestAppD7.pas *
(* = code is included in above link)
(JDGlassCommon.pas is shared in both DLL and Component)
Should look something like this:
NOTE: I'm not asking for a re-write, although you're more than welcome to. I just need some tips on how to approach this.
PS: Original glass drawing code credited to "NGLN" of StackOverflow answering a prior question of mine: Delphi custom drawing - glowing glass
Should I keep using Records as I am, or use something else
like Packed Record?
Records are good for interop. Don't pack them, that just makes interop harder.
Any tricks to use something other than String or PChar in
these Records?
Don't use string. That's Delphi only and even specific to Delphi versions. PChar is fine for interop. Sometimes it can be simplest to use fixed length inline char arrays in records. It depends on the use.
How would I wrap this DLL in C#?
Call it from C# using p/invoke.
Is it safe to pass types such as TColor in the Records?
Yes that's easy to work with. Make sure it's a true RGB color rather than a special color like clWindow.
Do you foresee any other issues in my code?
The glass rendering may well be incompatible with the rendering used by the C# libraries. It could very well depend on whether or not your C# code uses WinForms or WPF. In fact you may well find that the C# developers would find it easier to use native C# code. I expect glass rendering is well supported in the common C# GUI frameworks.
I found a fairly complex function in a greasemonkey script that I would like to use in my C# app. Basically I am parsing a page and I need to collect all or 4 members of var avar = {}; (i haven't done this yet but they are all strings using var avar.name = "val")
Then I need to call the gm func which returns a string and takes in 3 strings. How can I call the function in C#? I am using .NET 3.5
I'm assuming that you are after some code-reuse on the server-side or in some other freestanding app that processes HTML data.
You can compile (at least a subset of) JavaScript in .net using the Microsoft.JScript.JScriptCodeProvider class -- though note that the class warns
This API supports the .NET Framework
infrastructure and is not intended to
be used directly from your code.
Once compiled the assembly generated (as specified by the CompilerParameters supplied to the provider) should be dynamically loadable. It would be advisable to examine the generated assembly with a tool like Reflector to see what it is you've actually generated, in terms of classes and namespaces.
Disclaimer -- I've only ever used this technique with the CSharpCodeProvider acting on C# source, but I would expect there to be a reasonable level of compatibility across .net languages for this sort of thing.
EDIT -- For an example of compiling JavaScript from C# see this blog post on Verifying JavaScript syntax using C#.
First, you probably want to consider exactly why you're trying to do this. Is it that you want to use the algorithm from the JS in C#? If so, go ahead. If you want to use C# in client-side code (i.e. the browser), go investigate Silverlight instead.
Second, I'm not sure that what you're trying to do is actually possible. Depending on what youre trying to achieve, you may be better off translating the Javascript from the Greasemonkey app into C# 3.5 (assuming that the script's licensing conditions allow this), for use in your app.
The translation shouldn't be hugely difficult - C# has been getting more and more like JS in the last few versions. Just watch out for the "var" keyword; it means something slightly different in C# to what it means in JS (contrast "type inference" in C# with "dynamic typing" in JS).
Of course, maintaining both versions of the code after you've done this will be tricky and painful. I recommend keeping 1 authoritative version of the code if you can.
Good luck!
Can you provide more information about your script and what you want to accomplish? Most Greasemonkey scripts interact with the DOM via the use of Javascript. You can run Javascript in C# but the DOM will not be available to you.
I have two application that need to talk to each other. App1 needs to be able to serialize an object that App2 can then deserialize. Easily done, right? Here's the problem; App1 is C# based, App2 is Java based. So App1 needs to write out the file in the Java binary file format. How can this be done?
The way I see it, I have two options. The first is figure out some way to serialize a Java object in C#, so that App1 just creates the appropriate file. My other option would be to write a converter in Java that reads in a file and populates the object accordingly and serializes the newly populated object. That way the C# app would only have to write out some sort of formatted text file that the converter then interprets.
I can't make any changes to the Java application.
How should this be done?
Update:
The Java application is already in the hands of customers so changing the serialization scheme would cause the customers existing data to be incompatible. The Java App uses the native java serialization when dealing with this object. Modifications to the Java app can't happen.
The C# app uses protocol buffers to serialize its own data.
The best answer is option 3:
use a language-neutral serialization scheme.
I use JavaScript. Thrift is another option, protocol buffers I believe are more focused on RPC, but should be usable for serialization as well. XML or a custom binary format would be other options.
Edit:
Sorry, didn't notice that you can't make changes to the Java application. That said, the best way to do it would probably be to create your own well defined format, write a java app that can read that format, then output a serialized java object for the legacy app.
"IKVM" might be something you could use. This product allows you to convert compiled java bytecode (.jar, etc.) into a .NET DLL. It's super easy to use, and might give you the interop you need.
Other than this, the easiest way to accomplish this without a binary-level interop is to just use a plain text format, such as a CSV or XML.
Just use XML serialization. Both frameworks have good support, and the simplicity will make it easier to debug / maintain. Write a small program in Java that just imports the XML and writes the binary file.
Your best bet would be to write something that uses Java Native interface. Not fun, but it'll work.
You can do this directly using JNI (not fun but doable) or there may be some tools out there that will generate code for you -- take a look at SWIG: http://www.swig.org/
You would call Java from C# to do the persistence for you.
Is there such a thing as an x86 assembler that I can call through C#? I want to be able to pass x86 instructions as a string and get a byte array back. If one doesn't exist, how can I make my own?
To be clear - I don't want to call assembly code from C# - I just want to be able to assemble code from instructions and get the machine code in a byte array.
I'll be injecting this code (which will be generated on the fly) to inject into another process altogether.
As part of some early prototyping I did on a personal project, I wrote quite a bit of code to do something like this. It doesn't take strings -- x86 opcodes are methods on an X86Writer class. Its not documented at all, and has nowhere near complete coverage, but if it would be of interest, I would be willing to open-source it under the New BSD license.
UPDATE:
Ok, I've created that project -- Managed.X86
See this project:
https://github.com/ZenLulz/MemorySharp
This project wraps the FASM assembler, which is written in assembly and as a compiled as Microsoft coff object, wrapped by a C++ project, and then again wrapped in C#. This can do exactly what you want: given a string of x86/x64 assembly, this will produce the bytes needed.
If you require the opposite, there is a port of the Udis86 disassembler, fully ported to C#, here:
https://github.com/spazzarama/SharpDisasm
This will convert an array of bytes into the instruction strings for x86/x64
Take a look at Phoenix from Microsoft Research.
Cosmos also has some interesting support for generating x86 code:
http://www.gocosmos.org/blog/20080428.en.aspx
Not directly from C# you can't. However, you could potentially write your own wrapper class that uses an external assembler to compile code. So, you would potentially write the assembly out to a file, use the .NET Framework to spin up a new process that executes the assembler program, and then use System.IO to open up the generated file by the assembler to pull out the byte stream.
However, even if you do all that, I would be highly surprised if you don't then run into security issues. Injecting executable code into a completely different process is becoming less and less possible with each new OS. With Vista, I believe you would definitely get denied. And even in XP, I think you would get an access denied exception when trying to write into memory of another process.
Of course, that raises the question of why you are needing to do this. Surely there's got to be a better way :).
Take a look at this: CodeProject: Using unmanaged code and assembly in C#.
I think you would be best off writing a native Win32 dll. You can then write a function in assembler that is exported from the dll. You can then use C# to dynamically link to the dll.
This is not quite the same as passing in a string and returning a byte array. To do this you would need an x86 assembler component, or a wrapper around masm.exe.
i don't know if this is how it works but you could just shellexecute an external compiler then loading the object generated in your byte array.