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 Kommentare

Rik
Rik am 20 Sep. 2018
Why not make it a function?
Because a long script need lots of extra effort to make it function :)
Rik
Rik am 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
Adam am 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!
Thanks Rik and Adam !! Got lots of clarification with this discussion. Thanks a lot.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan
Jan am 24 Sep. 2018
Bearbeitet: Jan am 24 Sep. 2018
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
Walter Roberson am 24 Sep. 2018

0 Stimmen

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.

Kategorien

Gefragt:

am 20 Sep. 2018

Beantwortet:

am 24 Sep. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by