Can we create C code from m script (not from m function)??
显示 更早的评论
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 个评论
Rik
2018-9-20
Why not make it a function?
sanjeev kumar
2018-9-23
Rik
2018-9-23
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
2018-9-24
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
2018-9-24
回答(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
2018-9-24
0 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB Coder 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!