- globals... a major cause of beginners writing slow, buggy, impossible-to-read code. Globals are the least recommended way of passing data between workspaces.
- dynamic variable names... a major cause of beginners writing slow, buggy, impossible-to-read code. MATLAB has a whole page advising to avoid string evaluation and creating variable names.
Programmatically define global variables in MATLAB Coder
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone!
I am using the MATLAB Coder App to translate a MATLAB function to C code. In my code unfortunately I use several dozens of global variables. Is there a way I can programmatically define the global variables without having to do it manually?
Maybe there is a way to access a file were the App stores the Input Types and write into it directly instead of going over the App and insert every single global variable manually? Please excuse my ignorance on the use of MATLAB Coder.
Thanks in advance for your help!
BR
2 Kommentare
Stephen23
am 12 Jun. 2017
Consider that you want to mix both:
So lets combine the two worst code practices into one big mess of impossible-to-debug code, and win some obfuscated code prizes while we are at it.
Hanging a hurricane lamp out on the verandah is a great idea if you want to attract all of the bugs in the neighborhood. If you want to keep your verandah clean and tidy, do not use globals and dynamically defined variable names.
Read more:
Adam
am 12 Jun. 2017
I assume Matlab Coder simply doesn't support global variables either. I can't imagine how it would do, given they just parachute in from anywhere and can be edited anywhere.
Code often has to be reworked to get it to run through Coder (though globals shouldn't be in code for any purpose). In your case you'll have to change those global variables for more standard programming practices.
Antworten (1)
Damiano Schirinzi
am 24 Jan. 2018
I actually found a simple workaround to my problem above: (Skip MATLAB Support part to get straight to the Answer)
MATLAB Support told me, that there is no way to automatically add global variables as is on contrary possible to do with inputs to the function. Hence they told me that global variables have to be added manually in the MATLAB Coder guy. They also told me that they would start a feature request for a future Version of MATLAB Coder.
But actually there is a workaround. Whenever you start the MATLAB Coder App and start working on a function, e.g. yourFunction.m, MATLAB Coder will create a file yourFunction.prj. You can then open this file in an editor and it will present itself in xml-format. There you will be able to find a paragraph that will look loke this:
<param.globals>
<Globals>
<Input Name="g">
<Class>double</Class>
<Size>1 x 1</Size>
<Value />
<InitialValue />
<UserDefined>true</UserDefined>
<Complex>false</Complex>
</Input>
</Globals>
</param.globals>
Now just write a script adding automatically further global variables instead of having to type them in. ;)
I am aware of the fact that you should never use global variables when programming and believe me I was the last person that wanted something to do with a code that used over 200 variables!! But that s how I received it and rewriting the code would have been too time consuming. It was a make it or brake it deal and it actually worked out pretty well. MATLAB Coder is an amazing tool!
Footnote: MATLAB Coder will write you after the translation to C-code a library, probably yourFunction_variables.h which will contain your global variables, now of course called external and it will create you also a yourFunction_initialize function which you can call to have your external variables initialized. Indeed a very handy feature.
Pay attention to any persistent variables in your matlab code, they will also be translated to external variables loosing their reduced scope and becoming simple global variables instead!
1 Kommentar
Adam
am 25 Jan. 2018
Be sure to test as many scenarios as you can though. Matlab Code can indeed be a bit like magic. As far as I am aware it can be trusted to faithfully reproduce results that you got with the Matlab code, but beware that this also means it will faithfully reproduce any bugs that were in the Matlab code and global variables are like a magnet for bugs. I understand it is code you were given rather than wrote yourself, which is never an easy thing to deal with, just be aware that the same flaws that globals cause in Matlab will be ported to the C code, though at least if it is sealed code nothing new should be able to change those global values.
Siehe auch
Kategorien
Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!