Is it possible to run a MEX file from the same code that generates it using MATLAB Coder?

1 次查看(过去 30 天)
I am converting a subset of MATLAB files from a GUI into MEX files. I want to provide the user the option to run the original MATLAB files or the MEX files once they have been generated. I imagined that the user could select which they would like to from the GUI.
There are a number of ways to go about this, but I thought that the following would be the least "bulky" in terms of code: using the same MATLAB file to either a) generate a MEX function, b) run the MATLAB code, or c) run the MEX code. Below is a code example of how I thought to implement this.
function output = my_func(input1, input2, code_flag, matlab_flag)
% skip coding out the mex function
coder.extrinsic('my_func_mex');
if code_flag == 1 || matlab_flag == 1
% if coding out using codegen or running original MATLAB code
% output the sum of the inputs
output = input1 + input2;
else
% run the mex file and return its output
output = my_func_mex(input1, input2, code_flag, matlab_flag);
end
The call to generate the MEX function would be done outside of this file using the following command:
codegen my_func -args {coder.typeof(0, [1, 5], [0, 0]), coder.typeof(0, [1, 5], [0, 0]), 1, 1}
I am able to compile the code and build the MEX function, but if I try to run my_func with the flags set to false, i.e. run the MEX version, MATLAB crashes. Why? Is what I am trying to do not possible? If not possible, why?

采纳的回答

Magda Bielinski
Magda Bielinski 2016-1-25
It is possible...here is the corrected function code to do so.
function output = my_func(input1, input2, run_matlab)
if run_matlab == 1 || coder.target('MEX')
% if coding out using codegen or running original MATLAB code
% output the sum of the inputs
output = input1 + input2;
else
% run the mex file and return its output
output = my_func_mex(input1, input2, run_matlab);
end
The call to codegen in the question remains the same.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Generating Code 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by