Declaring const variable in generated code

5 views (last 30 days)
Matthew on 9 Aug 2013
Edited: Mike Hosea on 2 Nov 2013
I'm trying to generate in matlab C-code that declare and define with const value a variable, e.g.: 'const int variable = 4;'
I want it to be visible from other C-file (not generated) via 'extern' keyword.
The problem is that without further using variable in matlab source The Coder omitt putting it's declaration at all.
Using coder.opaque('int', '4') also don't help. Declaring is as 'global' unfortunally as well.
Can I do this other way than filling 'Custom C code...' field? I believe it's an 'ugly' way to do that.
I have matlab 2012b

Answers (1)

Mike Hosea
Mike Hosea on 2 Nov 2013
Edited: Mike Hosea on 2 Nov 2013
This is a common request in one form or another. A common variant is that people would like to have numeric constants #defined rather than folded into the code as literals.
I wish I had a better answer for you today, but the Custom C Code solution is what I would probably do. I might write a build script that generated a header "on the fly" based on the current setting to keep things from getting out of sync. The build-script can also save the current value to a .mat file, which can then be loaded from withing the function(s) being compiled using coder.load.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!