Evaluate a list of functions without using feval in generated code for embedded application

4 ビュー (過去 30 日間)
I want to generate a mex function to evaluate a list of matlab functions iterativeley. The functions to evaluate are stored in a cell variable 'functionsList', each element being the name of the function file (char type). The code below works fine but it uses the 'feval' function which is an extrinsic one.
function outputs = functionToGenerate(inputs)
global functionsList
outputs = inputs;
for i=1:length(functionsList)
outputs = feval(functionsList{i},outputs);
end
end
As the final goal is to use this code for embedded application I am looking for an alternate method. I tried to use the 'str2func' with the code below :
function outputs = functionToGenerate(inputs)
global functionsList
outputs = inputs;
for i=1:length(functionsList)
f = str2func(functionsList{i});
outputs = f(outputs);
end
end
but I get an error during code generation :
Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression.
This error concerns the line using 'str2func'. Can anyone could tell me where does the error come from and how can I solve this?

採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 26 日
You are not going to be able to do this with embedded coder. Being able to do this would require being able to read and execute .m source file at execution time, which is not possible on embedded processors.
I do not recall at the moment if it is possible to use function handles with embedded coder.
You need to build the functions into your program as a hard-coded list of sources. Then at execution time, you would parse the data file as a text file and match it to a known function name.
If you are not able to use an array of function handles to dispatch the appropriate routine, then you can use a switch() statement to make the appropriate call.
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 10 月 26 日
Suppose at the time of code generation, the text file contained
subtasks/3/mytask3b.m
subtasks/7/mytask7a.m
subtasks/3/mytask3a.m
and you generate code for those functions and bundle it up and so on. All nice -- but then the user changes the text file to
subtasks/3/mytask3b.m
subtasks/7/mytask7a.m
bessiesmith/blues/sing.m
subtasks/3/mytask3a.m
then under your requirements, at run time the code would need to compile and execute bessiesmith/blues/sing.m . That is not something that can happen on an embedded system.
One of the alternative formulations:
function execute_steps_from_file(filename)
fid = fopen(filename,'r');
while ~feof(fid)
thisline = fgetl(fid);
switch thisline
case 'subtasks/3/mytask3a.m'
mytasks3a();
case 'subtasks/3/mytask3b.m'
mytasks3b();
case 'subtasks/7/mytask7a.m'
mytasks7a();
case 'subtasks/7/mytask7b.m'
mytasks7b();
otherwise
...
end
end
Now there are no feval(), no need for run-time compilation, and no possibilities an altered text file could lead to an attempt to execute something not defined.
David Morin
David Morin 2017 年 10 月 27 日
I see your point. I must be able to manage something with this.
Thank your very much for your explanation!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by