Create a mex file of functions which are using in matlab app designer
11 views (last 30 days)
Show older comments
Having a function(Sample.m) which is called in pushbuttoncalllback changed value ,so i need to convert that function into mex file , that function contains data extraction function calls and plotting .
Using matlab coder , my entry point function is Sample.m , and in define input types , i am loading my mlapp file which is converted to .m file , so while loading this i am getting error class MAR_140_CAN_exported is not supported by coder. Types as it is a handle class?
0 Comments
Answers (1)
dpb
on 22 Mar 2025
Moved: dpb
on 22 Mar 2025
MATLAB Coder builds C/C++ code that is either turned into a standalone application of its own or used to build libraries or classes that can be called from other C/C++ applications. It does NOT build MATLAB .mex functions to be called from MATLAB; that is done with mex. MEX files are written in C/C++/Fortran/... but require a "glue" interface routine to pass arguments to the function and return outputs to MATLAB.
You would have to write that interface routine and have it call the output C code from the app. Unless it has been demonstrated that the m-file code runs too slowly, it would be far simpler for development and ongoing code maintenance to leave the code in MATLAB; when the app is packaged it won't be accessible to end users anyway if that is the concern.
The statement that the function contains both data extraction and plotting also makes one wonder if it would be wise to further modularize the code into separate functions--those that deal strictly with the data acquisition/processing and then call the plotting routines.
We would need more information on the actual content of this function and why you think it needs to be turned into a mex file to comment specifically, but my strong recommendation would be to first ensure it has been optimized in MATLAB syntax/performance and then, only if it has unacceptable performance issues, consider mex.
2 Comments
dpb
on 24 Mar 2025
Edited: dpb
on 24 Mar 2025
See <Techniques to Improve Performance> for general tips; specifics would depend on the implementation of the code itself. First and foremost, however, is to profile the code to find where the bottleneck(s) actually is(are).
"We would need more information on the actual content of this function ... to comment specifically..."
Note that first-time loading of an app is always slow and mex won't fix that...but, the one specific recommendation I made earlier may help at least a little.-- from the link above."Use modular programming. To avoid large files and files with infrequently accessed code, split your code into simple and cohesive functions. This practice can decrease first-time run costs."
If your statement above is accurate that everything is in one function, it's almost certain it should be refactored.
See Also
Categories
Find more on MATLAB Compiler 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!