Can we create C code from m script (not from m function)??
Show older comments
Actually I have a m script not m function which I want to to generate c/cpp code,can we do if yes how??
5 Comments
Rik
on 20 Sep 2018
Why not make it a function?
sanjeev kumar
on 23 Sep 2018
Rik
on 23 Sep 2018
Does it though? You can just put function at the top and end at the bottom. Sure, that would not be following best practice, but a long script doesn't either.
Adam
on 24 Sep 2018
Well, if it takes a lot of effort that implies there are a lot of variables that are just expected to already exist in the workspace when it runs and if that is the case then clearly you can't expect to have it converted to C/C++ code, as Jan says, C/C++ code needs an entry point. If you don't have lots of hidden assumptions on variable that need to already exist then, as Rik says, it is trivial to add one line to the top of your script. You don't even need the end at the bottom if you are really averse to the extra work!
sanjeev kumar
on 24 Sep 2018
Answers (2)
It is impossible. A C/CPP code must receive some inputs and provide outputs, but scripts do not have a defined input/output interface. In consequence only functions can be converted to C-functions. There are no "C-scripts".
Because a long script need lots of extra effort to make it function :)
Writing "long scripts" is a general mistake and it impedes writing and debugging the code - as well as converting it to C.
Walter Roberson
on 24 Sep 2018
0 votes
MATLAB Coder refuses to use a script as the entry point.
For a number of years, MATLAB Compiler also refused to compile scripts as the entry point. I saw hints that might have changed a few years ago, with it effectively just tossing on a function line that it generated, but I am not certain of this.
For reasons that others discussed, compiled code really should have a function header to define the interface.
If the situation happened to be that you had a very long script and the time to parse it for interactive execution was becoming a problem, then an option would be to pcode it as pcode pre-parses. But in such a case the code would almost certainly be better rewritten. And this case would have little relevance to the MATLAB Coder use case.
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!