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
Related
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.
I have a nice .net assembly of core matlab functions, created of course with the matlab compiler. For functions that accept numbers or arrays of numbers, this is fine; I can write code in c# without having to revert to matlab (well, the RCM has to be installed; that’s fine).
For functions that must reference other functions, however, the only way I can find so far to get a c# programme going is to compile both functions into the assembly. To explain better, let’s say I have a library in which I’ve stored the ode45 routine. If I want to solve a specific equation, let’s say something simple like dy/dx = -y, then I have to create a matlab script file which may be written as follows:
function dydx = diffeq(x, y)
dydx = -y
[obviously the analytical solution exists, but for the sake of this example let’s say I want to solve it this way]
Now in order to solve this equation, I would have to add this function as a method in my class to be compiled into the .net assembly. This of course ruins the generality of my library; I want application-specific equations in a different library to my core math function library. That is, the ODE45 method should reside in a “more core” library than the library in which the “diffeq” method would reside.
More than that, I would much prefer to create the “diffeq” method in a c# class that I can edit directly in e.g. VS2012. I would like to edit the equation directly rather than having to enter matlab each time and recompile an assembly.
To solve this problem, I have gone to the extent of decompiling the assembly which contains both the ode45 code and my differential equation method; it turns out the assembly is nothing but an interface to the MCR; the diffeq methods in the assembly return something like the following:
return mcr.EvaluateFunction(numArgsOut, “diffeq”, new object[0]);
We note that the function/method “diffeq” is not part of the MCR; MCR does not change. However, I can’t find the equation anywhere in the assembly.
Which begs the question “Dude, where’s my function?”
There is a ‘resources’ component of the assembly in which we find [classname].ctf, and in that we’ll find some machine code. This looks encrypted, but the equation might be hidden in there. If so, that would be a deliberate attempt to prevent when I am attempting, and kudos to MathWorks for making it impossible for me to avoid having to enter the matlab application!
However, there doesn’t seem to be anything in licensing to prevent what I want to do; I think it would be great if mathworks would allow as open an approach as that, but in the interrim, does anyone know how to do this?
The "MATLAB Compiler" has a somewhat misleading name. It is more of a deployment solution than a compiler in the actual sense (see note below). It is mainly intended to distribute MATLAB applications to end-users without requiring a full MATLAB installation on their part (only the royalty-free MCR runtime needs to be installed).
The MCR is in fact a stripped-down version of the MATLAB engine along with accompanying libraries.
When you use MATLAB Compiler to generate a binary package, the result is a target-specific wrapper (be it a standalone application, C/C++ shared library, Java package, or a .NET assembly) that calls the MCR runtime. The binary generated includes an embedded CTF archive containing all the original MATLAB content (your M-files and other dependencies) but in an encrypted form. When first executed, the CTF archive is extracted to a temp folder, and the M-files (still encrypted) are then interpreted by the MCR at runtime like typical MATLAB code.
There is an option in deploytool (mcc -C) to tell the compiler not to embed the CTF archive inside the binary as a resource, instead to place it as a seperate file next to the generated binary (this CTF archive can be inspected as a regular ZIP-file, but the source files inside are still encrypted of course).
See the following documentation page for more information:
Application Deployment Products and the Compiler Apps
PS: The truth is MATLAB Compiler started out as a product to convert MATLAB code into full C/C++ code which used the now discontinued "MATLAB C/C++ Math Library" (no runtime requirement, you just compile the generated C++ code and link to certain shared libraries; the result is a true compiled executable not a wrapper). This functionality completely changed around the time MATLAB 7 was released (the reason being that the old way only supported a subset of the MATLAB language, while using the current MCR mechanism enables deploying almost any code). Years later, MATLAB added a new product to replace the once-removed functionality of code translation, namely the MATLAB Coder.
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.
Ok, so I was wondering how one would go about creating a program, that creates a second program(Like how most compression programs can create self extracting self excutables, but that's not what I need).
Say I have 2 programs. Each one containing a class. The one program I would use to modify and fill the class with data. The second file would be a program that also had the class, but empty, and it's only purpose is to access this data in a specific way. I don't know, I'm thinking if the specific class were serialized and then "injected" into the second file. But how would one be able to do that? I've found modifying files that were already compiled fascinating, though I've never been able to make changes that didn't cause errors.
That's just a thought. I don't know what the solution would be, that's just something that crossed my mind.
I'd prefer some information in say c or c++ that's cross-platform. The only other language I'd accept is c#.
also
I'm not looking for 3-rd party library's, or things such as Boost. If anything a shove in the right direction could be all I need.
++also
I don't want to be using a compiler.
Jalf actually read what I wrote
That's exactly what I would like to know how to do. I think that's fairly obvious by what I asked above. I said nothing about compiling the files, or scripting.
QUOTE "I've found modifying files that were already compiled fascinating"
Please read and understand the question first before posting.
thanks.
Building an executable from scratch is hard. First, you'd need to generate machine code for what the program would do, and then you need to encapsulate such code in an executable file. That's overkill unless you want to write a compiler for a language.
These utilities that generate a self-extracting executable don't really make the executable from scratch. They have the executable pre-generated, and the data file is just appended to the end of it. Since the Windows executable format allows you to put data at the end of the file, caring only for the "real executable" part (the exe header tells how big it is - the rest is ignored).
For instance, try to generate two self-extracting zip, and do a binary diff on them. You'll see their first X KBytes are exactly the same, what changes is the rest, which is not an executable at all, it's just data. When the file is executed, it looks what is found at the end of the file (the data) and unzips it.
Take a look at the wikipedia entry, go to the external links section to dig deeper:
http://en.wikipedia.org/wiki/Portable_Executable
I only mentioned Windows here but the same principles apply to Linux. But don't expect to have cross-platform results, you'll have to re-implement it to each platform. I couldn't imagine something that's more platform-dependent than the executable file. Even if you use C# you'll have to generate the native stub, which is different if you're running on Windows (under .net) or Linux (under Mono).
Invoke a compiler with data generated by your program (write temp files to disk if necessary) and or stored on disk?
Or is the question about the details of writing the local executable format?
Unfortunately with compiled languages such as C, C++, Java, or C#, you won't be able to just ``run'' new code at runtime, like you can do in interpreted languages like PHP, Perl, and ECMAscript. The code has to be compiled first, and for that you will need a compiler. There's no getting around this.
If you need to duplicate the save/restore functionality between two separate EXEs, then your best bet is to create a static library shared between the two programs, or a DLL shared between the two programs. That way, you write that code once and it's able to be used by as many programs as you want.
On the other hand, if you're really running into a scenario like this, my main question is, What are you trying to accomplish with this? Even in languages that support things like eval(), self modifying code is usually some of the nastiest and bug-riddled stuff you're going to find. It's worse even than a program written completely with GOTOs. There are uses for self modifying code like this, but 99% of the time it's the wrong approach to take.
Hope that helps :)
I had the same problem and I think that this solves all problems.
You can put there whatever code and if correct it will produce at runtime second executable.
--ADD--
So in short you have some code which you can hard-code and store in the code of your 1st exe file or let outside it. Then you run it and you compile the aforementioned code. If eveything is ok you will get a second executable runtime- compiled. All this without any external lib!!
Ok, so I was wondering how one would
go about creating a program, that
creates a second program
You can look at CodeDom. Here is a tutorial
Have you considered embedding a scripting language such as Lua or Python into your app? This will give you the ability to dynamically generate and execute code at runtime.
From wikipedia:
Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. These behaviors can be emulated in nearly any language of sufficient complexity, but dynamic languages provide direct tools to make use of them.
Depending on what you call a program, Self-modifying code may do the trick.
Basically, you write code somewhere in memory as if it were plain data, and you call it.
Usually it's a bad idea, but it's quite fun.
Any astronomers out there? I'm wondering if anyone has produced or stumbled upon a .NET (preferably C#) implementation of the US Naval Observatoru Vector Astrometry Subroutines (NOVAS).
I know nothing (of consequence) about astronomy, and absolutely nothing about NOVAS, so please take this with a grain of salt.
But, I did look at the website, and it looks like they have a C implementation. You could always take the C implementation, access it via pinvoke, and write a C# wrapper around it.
Are you only interested in a port of that library or anything usable from C# for astronomy?
I don't have anything for the first part, but for the second I would take a look at AGI's Components. Their libraries provide ways to compute all kind of astronomical data. The Dynamic Geometry Library lets you model everything including planets and such rather easily.
This download contains a very useful astronomical library in C#.
Sorry that I don't remember where I got it but perhaps it is documented in there somewhere.
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=8399&lngWId=10
Sidenote: The NOVAS library is not very complete. You would be better off to pursue the SOFA lib from the International Astronomy Union. Here's the link:
http://www.iausofa.org/
Urania is an astronomy library in C#:
http://www.smokycogs.com/blog/tutorials/astronomical-calculations-in-c-sharp/
The download is the non-obvious "here" link on the page that combines all of the sample code into a single app called Urania.
Once downloaded, you will also need to modify the Urania.sln file to fix the paths of the different libraries that he uses (MathLib, UraniaLib, etc.) and then it will compile correctly. (Open Urania.sld in notepad and delete: "..\Libs\" out of the 3 project paths)