Alternatives to extrinsic functions such as imread and other functions during code generation in MATLAB - matlab

As you may know, extrinsic functions are not outputted during the code generation process. Are there alternatives to these functions and/or solutions to this problem? My code generation error report is shown below :
Code Generation Error Report
I am surprised that I can't output size and rgb2gray either. Since these are essential to my program, I cannot avoid them.
Help will be much appreciated!

This is a good question, and I see similar questions fairly frequently. As I started using MATLAB Coder, one of the biggest pitfalls was the constant search for supported functions. I sympathize with your frustration, and I have a few tips, having been through this.
First, to your direct question, while imread isn't supported by Coder, size and rgb2gray are. Probably Coder is complaining about these because they have been passed mxArrays from the call to imread, which is fine when it is extrinsic, but not ok for separate generation. That's just a guess. A very useful tool in writing code is the list of Coder supported functions: List of Functions supported in MATLAB Coder
But even with those two, to replace imread is not a tiny task. You'll have to find another library that supports the particular file you're working with, and then stitch that in using coder.ceval. Alternatively, if you can find a pure MATLAB implementation of it, that might help.
Are you targeting a pure C library or a MEX file? If you intend to use this code within the MATLAB environment, you can always use imread separately and then pass the data.
And now to some more general observations: MATLAB Coder isn't a perfect MATLAB to C translation system. It's extremely powerful, and I've been able to write some very large projects with it, but if what you want is the ability to run any MATLAB code without MATLAB around, you should look at MATLAB Compiler, a different add-on. There's a very good Q and A about this here: MATLAB Compiler vs MATLAB Coder
When writing projects in MATLAB Coder, it's really best to start from scratch, knowing you're targeting C code ultimately. There are so many gotchas in the conversion from MATLAB to C that you have to be always vigilant while writing the MATLAB code.
One tool that helps is to right-click on a file in the "Current Folder" list that usually resides on the left-side of the main window, and select "Check Code Generation Readiness." You'll get a great report of potential problems in the file. I recommend using this often.
Another useful tool is to always put the %#codegen tag into your code. This alerts the MATLAB editor that the .m file is intended for code generation, so it provides extra context-sensitive information while you're writing the file. This helps enormously.
The most commonly missing functions for code generation are file IO functions. There are some good reasons for this, but it's frustrating nonetheless.
When you stitch in external C code, you use the coder.ceval function, which can provide excellent access to external libraries. Using this well is a whole other topic, outside the scope of this question.
If you can indicate exactly what kind of files you're interested in reading (PNG, BMP, TIFF, etc.) perhaps someone may be able to identify a good external library for you to use.

Related

Hooking AMPL with Matlab using amplfunc and spamfunc

I'm interested in hooking up AMPL with Matlab to obtain objective values, gradients, and Jacobians to test ideas for optimization algorithms on a wide set of problems. I'm aware that I can use amplfunc.mex or spamfunc.mex to do this; however, I'm having trouble compiling the .mex files using the old netlib files on Windows. The link also given in a previous question:
Using MATLAB with AMPL
is outdated. Does anyone know where I can obtain these .mex files or offer any alternative solutions? Thanks in advance.
The mex files amplfunc.c and spamfunc.c are still available at http://www.ampl.com/netlib/ampl/solvers/examples. We aren't aware of a reason why these wouldn't compile, but if you are having trouble you could write to the AMPL user forum, ampl#googlegroups.com. Be sure to explain which function you are using and to give the complete text of any error messages you see.

How can I use matlab source code in labview without matlab?

I found mathscript can do this,but it seem too slow.
So I want to use matlab to create a dll file used by labview,but all information I can find just how dll could used in C++,but C++ also must use the .lib file ,I am concerned labview could not use lib. file.
So can I use the .dll in labvew,and how?Is there anyother way to use matlab source code in labview without matlab?
You will need to purchase the MATLAB Compiler which is quite expensive unless you are a student.
You can also call the MATLAB application (if it's installed) using the MATLAB Script Node (which is different than Mathscript) - see here
Another alternative is Python's Numpy which I personally picked up quickly coming from a MATLAB background. If you want to go down that route I can give you further advice on how to integrate with LabVIEW.

Will Matlab standalone be faster than Matlab from UI for long execution code?

I have built an standalone Matlab application. I was expecting it to be faster than running the application from the Matlab environent but it is indeed a bit slower (1.3 seg per iteration vs 1.5 seg per iteration)
I am not counting the init time required by MCR but the execution of my code.
Is that the expected performance or should I be obtaining a performance improvement?
I haven't found any settings on the deployment tool that could help to reduce execution time.
Thanks in advance
Applications built with MATLAB Compiler should execute at pretty much exactly the same speed as within MATLAB.
MATLAB Compiler does not convert your MATLAB code into machine code in the same way as a C compiler does for C. What it does is to archive and encrypt your MATLAB code (note, it properly encrypts it, not just pcodes it as a comment suggests), create a thin executable wrapper and package them together, possibly also with MATLAB Compiler Runtime (MCR). MCR is very similar to MATLAB itself, without a graphical user interface, and is freely redistibutable.
When you run the executable, it dearchives and decrypts your MATLAB code and runs it against the MCR. It should run exactly the same, both in terms of results and speed.
Very old versions of MATLAB Compiler (pre-version 4.0) worked in a different way, converting a subset of the MATLAB language into C code, and compiling this. This provided a potentially significant speed-up, but only a subset of the language was supported and results, unless you were careful, could sometimes be different. Similar functionality is now available in the separate MATLAB Coder product.
There are a few small things you can do to improve performance: for example, within deploytool you can specify which toolboxes your application uses. deploytool uses a dependency checker to package up all MATLAB functionality that it thinks your code might possibly depend on, but it can't always tell exactly, as the functions your code needs might change at runtime. It therefore errs on the side of caution and includes more than necessary. By specifying only the toolboxes you know to be necessary, you can speed things up a little (it also speeds up the build process quite a bit).

Can anyone with access to the new "Matlab Coder" product show some output of the translation to C?

Matlab Coder is a recently released MathWorks product. My understanding is that it is a Matlab-to-C compiler with the biggest advantage over previous solutions being that the resulting program does not need to be linked against a Matlab shared library.
Can someone with access to this product confirm the above? What are the dependencies of the translated programs and what kind of performance are we talking about? Also I would really like to see some example outputs, to know if the resulting C programs can be understood and improved without access to the Matlab source.
If done right this could be very powerful, allowing rapid prototyping in Matlab and instantaneous conversion to C when things are getting serious. I kind of whish it doesn't work well so that Python+Numpy+Scipy.weave is still superior ^^.
MATLAB Coder can allocate memory using malloc, so you can generate C code from MATLAB functions that operate on dynamically sized data. You can also choose the option of static allocation with a maximum size for variables.
RE: using BLAS for matrix multiplication – while the generated C code doesn’t automatically include any processor/platform specific optimizations, there is a feature called Target Function Library, which that allows users to write their own implementation of primitive operations (such as matrix multiplication), and include them in the generated code. You can hook up BLAS libraries to MATLAB Coder via that method. There’s also an ability to include optimized processor specific calls for larger functions through custom code integration and conditional compilation that lets you specify one set of code to use for code generation, and another set for simulation (for example, an optimized FIR function for an Texas Instruments DSP and functionally equivalent code for simulation that can execute on your PC written in C or in MATLAB).
Hope this is helpful --
Arvind Ananthan; Product Manager for MATLAB Coder; MathWorks
I am using Matlab Coder with Real Time Workshop (RTW) in order to generate self standind standard C code.
First of all you are asked to use a Matlab subset called "Embedded Matlab" you can find the doc about it on the web
You also have to avoid any dynamic exploitation of variables and you can't obviously generate c code for plots or figures.
The code it generates could be a mess to understand but it works.
you should actually not try to understand it. In a certain way it is as you would try to understand the assembler your compiler generates from a C code you wrote, quite pointless.
another thing you should take care of is to declare persistent big data types (vectors, big arryes, etc.) otherwise they will be allocated into your stack...
good luck!

Matlab vs Aforge vs OpenCV

I am about to start a project in visual image-processing and have no had experience with Matlab, Aforge, OpenCV and was wondering if anyone had any experiences with these different software packages.
I was also wondering which of the three packages were most efficient I assume OpenCV but has anyone had any experience?
Thanks
Jamie.
The question you need to ask yourself is which is more important - your time or the computer's time. If your task is really simple, you may be able to code it up in MATLAB and have it work right off the bat. MATLAB is by far the easiest for development - a scripted language with built-in memory management, a huge array of provided functions, and a great interface for displaying and manipulating data while debugging.
On the other hand, MATLAB is at least an order of magnitude slower than compiled openCV code for many tasks. This is especially true if you use the intel performance primitives libraries.
If you know how to code in MATLAB, I would suggest writing and debugging your algorithms in that language, then porting them to c/c++ with openCV for speed. If there are only a couple of simple functions that you need to speed up, you can call c code from MATLAB, but it's hard to get this working right the first few times you try it, so you're probably better off just rewriting your finished code entirely in c/c++
First, please elaborate about your project's needs. It has the biggest impact on the choice, in addition to other factors - your general programming knowledge (If you haven't dealt with dot net but just with C++, AForge is not a good choice, for example).
Generally,
Both AForge and OpenCV has a built-in interface to .Net, and OpenCV also with C++, python, and more. Matlab might be more efficient, but if you don't have any experience with it - you should also learn its syntax. Take it into consideration.
Matlab probably has the largest variety of functions, but it is more complicated than the other projects. OpenCV and AForge themselves have some differences - see them described in this StackOverflow question/ answers.
I worked last year in two similar projects with cars on the highway. Afaik, Matlab allows to process only one picture frame at a time (surely you could elaborate an algorithm to compute a stream) but using Simulink you can process the stream directly.
On the other hand, i found AForge a lot friendlier and easier to use since you can easily adjust the processing parameters from a GUI (not so fast/easy) to do in Matlab/simulink.
I'd go for Aforge.Net. It's also fast enough if you're worrying about processing speed. (using 640x480)
If you are asking about using one of these in .net,easily you can get info by this:
1-matlab mostly used in simulation of projects not the End-prototype project; my numer : 30;
2-aforge (as I'v used in many project) if you do not need the circular process like capturing image, or recognition of something in images or ... you'll find it very good, cause it is easy to use but useful for single processes; my number : 50
3-opencv very good at speed and useful for circular processes, for example you can capture images from a webcam and Instantly cartoonize it without any delay, But not easy-to-use as aforge. I like it anyway cause of its speed and MANY functions it gives us mostly anything we need in programming; my number : 80
Dr.Taha - Tahasoft.net