Hey guys, I'm currently working on a standalone app and I have to write my s-function in C language. I also have the s-function written in Matlab and I use the @(x) function. Thus, I was wondering if this function exist in C language?

 Réponse acceptée

dpb
dpb le 5 Juil 2022
Modifié(e) : dpb le 5 Juil 2022

1 vote

Standard C does not include what is generically called a "lambda" outside MATLAB.
GCC has an extension but I've never used it.
The generic thing is to write and include a named function in place of the anonymous function.

8 commentaires

Alright, thank you. Also, I assume that the matlab function fsolve() doesn't exist in C language as well?
dpb
dpb le 5 Juil 2022
Of course not.
See the 'Extended Capabilites" section of the doc to see the toolsets you can use to compile MATLAB code using such functions -- or, you have to use the API and call MATLAB from C code -- this requires having MATLAB to run such, of course, so depends on the end result needed and environment in which you plan to use this function.
Does it work with only Matlab Runtime installed ?
dpb
dpb le 5 Juil 2022
In one form or another, of course. All the compiler does is encode a bunch of links to subroutine calls; it does NOT compile/link machine code.
If you write C using MATLAB functions, somewhere has to be access to the runtime to execute those.
To write standalone C without the runtime, you've got to code the functionality directly all yourself (or find open source libraries that can compile/link to your code. It is having the latter all conveniently bundled for you into a comprehensive package that is what MATLAB is that makes it the toolset it is. In the olden days, one could find most of the base functionality in various places and gather all that together on one's own, but that was a formidable effort.).
40 years later, there are now alternate tools of varying types and completeness, but still putting it altogether is where MATLAB has its niche.
Alright, sincerely, thank you very much dpb. Really appreciated !
Steven Lord
Steven Lord le 5 Juil 2022
There are two somewhat related (conceptually) products being discussed in the comments, and I'd like to detangle them a little bit.
Using MATLAB Compiler for app deployment does require the MCR.
Using MATLAB Coder to generate C or C++ code from MATLAB code doesn't require the MCR.
If MATLAB Coder doesn't require the MCR, why use MATLAB Compiler instead of MATLAB Coder? The list of functions supported for use in MATLAB Compiler apps is larger than the list of functions supported for use with MATLAB Coder. As an example, few if any graphics functions are supported in MATLAB Coder but many are supported in MATLAB Compiler apps.
To check if a function is supported for use with MATLAB Coder, and what limitations that support has if any, check the Extended Capabilities section of its documentation page or apply the "C/C++ Code Generation" Extended Capabilities filter on the function reference list page.
Looking at the fsolve documentation page shows that it does support C/C++ Code Generation. I'm not sure off the top of my head when that support was introduced; if you're using a release prior to the most recent (currently release R2022a) check your local documentation's version of the fsolve page.
Thank you Steven. However, the work I have to do is to make a standalone app (with Simulink Compiler) of a simulink model using S-Function (written in C). Thus, if I already have my S-function written in Matlab language, can I just use Matlab Coder to generate this S-function in C language ? That'd save me a lot of time !
Steven Lord
Steven Lord le 5 Juil 2022
I haven't worked very much or at all with Simulink Compiler and S-functions but I believe the answer to your question is yes, assuming that all the MATLAB functions you're using in your S-function support C/C++ Code Generation and you're not violating any of the limitations for those functions in that mode.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink Coder dans Centre d'aide et File Exchange

Produits

Version

R2020b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by