2

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!

Dima
  • 38,860
  • 14
  • 75
  • 115
SDG
  • 2,260
  • 8
  • 35
  • 77
  • I've read around the internet that the combination of `fread` and `fopen` can be used instead of `imread`. However there isn't any documentation to support this. – SDG Apr 21 '15 at 02:39
  • I was wrong about there not being any support on the internet. [This](http://uk.mathworks.com/matlabcentral/newsreader/view_thread/284811) is a good forum post that deals with this problem. I'm going to try and use this. – SDG Apr 21 '15 at 02:42

1 Answers1

6

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.

Community
  • 1
  • 1
Tony
  • 949
  • 5
  • 20
  • I am solely using JPGs with my project. It certainly makes sense that the output from `imread` is causing the errors in the other functions. Should I seperately ask a question about stiching external C code using `coder.ceval`? – SDG Apr 21 '15 at 02:38
  • I also used `%#codegen` throughout my project and `imread` never came up as an error. – SDG Apr 21 '15 at 03:09
  • Also would I not get a C library when using *MATLAB Coder*? What exactly is the difference between *MATLAB Coder* and *MATLAB Compiler*? I am aware of Mex files being compatible with *GNU Octave* and *MATLAB*, but I do not understand how this relates to building a static/dynamic C library. – SDG Apr 21 '15 at 03:19
  • MATLAB Compiler allows you to produce a version of your project that can be run without a MATLAB license using the "MATLAB Compiler Runtime". I believe MATLAB Coder is trying to help you produce C code that you can compile and run without needing the extra runtime environment. – arcticmac Apr 21 '15 at 03:27
  • 1
    I edited the answer to include a link to another very helpful question showing the difference between Coder and Compiler. My guess is that you may really want Compiler for the work you're doing. – Tony Apr 21 '15 at 04:11