Matlab Coder vs hand coding?
Asked Answered
B

6

45

Some background for people reading this in the future (in case it's not locked). I tend to do my programming in a high level language to understand the problem first. After covering all possible corner cases I proceed to translating the code to C++ (or C).

Most of the code I write has maths in it and so MATLAB is the language I use (the alternative for me is Python). Anyway, I then translate the code from MATLAB to C++ by hand.

Does anyone know if there is any advantage/disadvantage in using MATLAB Coder? It's a new product and very expensive but apart from the time it saves in translation is there any other benefit?

Buckthorn answered 7/6, 2012 at 22:21 Comment(5)
Why don't you just download the MATLAB Coder trial, see how it works for you, and compare the results with previous work you've done?Kor
How about using a high-level language that's nice to work in, but still compiles to fast native code, right away? Haskell is quite good. C++11 isn't too bad anymore, either.Moisten
True, but Matlab has a lot of built in functions that are very convenient.Buckthorn
To what extend can you actually translate matlab code that uses built in functions and/or toolboxes. I understand that there are quite a lot of restrictions.Leishmaniasis
If going down the hand-coding route, C++ libraries/toolkits such as Armadillo can be very useful.Reste
S
49

Disclaimer

This is a very opinionated post based on my expirience for one particular project. I have not used the latest version of the coder, but I do have expirience with the equivalent product (embedded coder) for converting matlab code to C++ that was included as part of the former Real Time Workshop product. These comments should still apply. Your mileage may vary.

Early benefits...

In my situation, the embedded coder was used to make a processing block that fit into part of a larger audio application. The processing block had the job of processing a constant stream of sample buffers in real time. I made the original algorithm in matlab, and the conversion tool made it fairly simple to convert an early prototype into something that could be compiled to native code and used in a real time application. It was also nice to assume that the converted code was functioning numerically identically to the original without possibility of human error in the conversion process (assuming superhuman abilities of Mahworks engineers).

The benefits ended after this very early prototyping stage...

Problem 1: Wasting time interfacing

As the algorithm grew in complexity, i started worrying more and more about how to code the matlab interface to the function so that after conversion, it would be easy to interface with the C++ framework (I wanted to monitor the internal states in real time). This eventually started using as much time as the actual algorithm development itself, thus defeating the purpose of using such a tool. I could have broken down the algorithm into smaller chunks and then glued them together using C++, but then I'd loose the ability to have a direct Matlab-only comparison of he complete algorithm.

Problem 2: Not all functions are supported or supported fully

The coder supports a subset of the Matlab language. In some cases, supported functions are limited in some way. For example, in the application that I was working on, I wanted to be able to modify the characteristics of a filter in real time. I could not use the standard Matlab filter prototyping functions, because the code generation tool would not allow calls to the filter prototyping function with variable arguments. I ended up spending time with a DSP book developing my own implementation, even though we have a signal processing toolbox license.

Problem 3: Automatically generated code was inefficient

I got frustrated with the interface issues and coded the algorithm by hand in C++. For my application, there was a 75% performance boost in the favour of the hand written code over the converted code. Performance differences will be very different depending on your application, probably the version of the conversion tool used, and your fondness of your profiler. The conversion tool itself is a complex product that has many settings to learn. Trying to work out how to tweak settings and the matlab code to improve performance uses more time that could be spent hand coding.

I have not used the conversion tool since...

I now prefer a more test-assisted approach. I code a prototype in Matlab and tweak until I am sure that it behaves as I want it too. I then think in C++ and recode the algorithm in a way that is more natural to that language. I then make a mex file that interfaces with my C++ code so I can test it against my trusted matlab equivalent. For the problem space that I work in, this is a much more efficient (human and machine) way to get stuff done.

In conclusion, this is just the opinion of one user. Perhaps (as suggesred in a comment on your original post) you should sign up for the trial to see how you get along. However, if you are a bit of a C++ ninja, testing by building mex files does not require an expensive license for an add-on product and it will make you a better developer.

Saguache answered 7/6, 2012 at 23:52 Comment(3)
"Test-assisted" as you call it is standard practice. Keep the original matlab code for non-regression testing and you have a good and efficient workflow.Lowenstern
"Automatically generated code was inefficient" - do you remember what were the perf issues in generated code?Mide
@Mide definitely not almost a decade on 😅, and they'd probably be irrelevant in whatever product is out today even if I could rememberSaguache
D
2

If it is easier for you to write in MATLAB, then the value would greatly depend on how much you value your time.

Comparing MATLAB and C or C++ for performance is very complicated. C or C++ are going to be faster in most cases, but in some linear algebra applications it is possible that MATLAB will execute the fastest. I remember a professor that claimed he had FORTRAN applications that ran slower than the equivalent in MATLAB. There are a lot of case studies on this - I would recommend you look at the different studies comparing the speed that turn up in google and compare them to what you're doing to make your decision.

Deli answered 7/6, 2012 at 22:48 Comment(1)
I don't think he's asking whether it's worth it to convert from MATLAB to C but what's the best approach.Johnny
W
2

It's all about the process.

Where I work, we've developed a good management scheme for our Simulink models and their dependencies. Then I've developed a script to proceed with the autocoding step and a colleague developed project files in an IDE, such that upon running my script, all source files are dispatched to a proper folder structure and the project can be readily compiled in the IDE, where someone else also deployed a wrapper code to interface the autocoded software.

The trick (IMHO) is to automate your process the best you can as early as you can. By doing so, you can develop very complex models and then create C code for production in a couple hours. And you can update the models all you like, but the code keeps easy to maintain.

The downfall is that setting up this process is neither trivial nor completely generic (one size does not fit most).

Also, you should really put some testing in place to verify that the generated code does indeed represent the model you had. This is not guaranteed, and while I think Matlab Coder is pretty reliable, it is not error-free.

Watersoak answered 25/1, 2019 at 17:15 Comment(0)
R
0

As it has been said above it depends upon ur application. I tried to convert the decoder (of a communication system) it gives the accurate results but for large number of bits it is slower than its own MATLAB version. So my conclusion was to convert MATLAB code to C by hand.

Raiment answered 29/9, 2016 at 16:34 Comment(0)
L
-1

Advantage:

  1. A lot of complex math functions are available.
  2. For hardcore math programming related.

Disadvantage:

  1. It's not popular compared with another modern languages like C#, java, python . you name it.
  2. Since you code matlab, you tend to focus mostly on complex math problems solving. Another languages are used in variety tasks from drawing, web development and math as well ( it doesn't have rich math functions like matlab for sure )

Another benefits I know: Since it's optimized for technical programming, you may have better performance when writing application on this field. The performance is very dependable, take a look at this question, it provides some helpful information.

Levasseur answered 7/6, 2012 at 22:53 Comment(0)
M
-1

I think MATLAB has lots of limitation over normal C coding. I agree that there are so many inbuilt blocks which can be used directly but if you write a code in MATLAB then it will take nearly 5 times longer in comparison to C code because from defining variables to taking loops, switch cases, its very time consuming in MATLAB modeling

And suppose you have made a model using Simulink but when you try to add something then again it's very time consuming but in C it is just 2 minute task.

The next problem is that you can't comment out any piece of model as you do in other programming languages.

For a big project sometimes MATLAB gets crashed, correpted, sometimes hang and simulation in stateflow is like a sh*t.

In the end I just say one thing, that use MATLAB modeling (stateflow+simulink) only if you have lots of patience.

Maroc answered 7/10, 2012 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.