## Create functions based on character array in Matlab Function Block using str2func or eval

### Emerson Butler (view profile)

on 18 Apr 2019
Latest activity Answered by Emerson Butler

### Emerson Butler (view profile)

on 30 Apr 2019 at 17:45
Hello,
I have a string array in the base workspace which contains a list of equations:
equations =
2×1 string array
"x = s || t"
"y = s && t"
x and y are connected to a bus which is the output of a Matlab function block, s and t are connected to a bus which is the input of the Matlab function block. The string array "equations" is in my Matlab Workspace, and is created from reading another file. The equations will vary with each new file and have hundreds of entries. My plan is to use the str2func function in a Matlab Function block to convert these strings into equations, such that the Matlab Function block will use the inputs, s and t, to generate the outputs, x and y, based on the equations.
The inputs and outputs are structs which are coming from/to the busses, so I would have S1.x, S1.y, S2.s, and S2.t.
The equations are being generated from a different program. This program will ensure that all of the equation variables will exist in the busses I've created, and it will also ensure that the equations are all boolean algebraic expressions, which removes any risk of unintended outcomes from the equations.
I first convert the equation string array to characters, then to unsigned 8 bit integers.
eqts_int = uint8(char(equations));
eqts_int is fed into the Matlab function block through a constant block.
In the function block, I try to split the outputs from the equations, and set the outputs = str2func(inputs)
str = '@(s,t)s || t';
fh = str2func(str);
x = fh(s,t);
But this unfortunately results in:
Undefined function or variable '@(s,t)s||t'.
Function 'Subsystem_name/MATLAB Function' (#2400.620.627), line 34, column 15:
"fh(s,t)"
My question is whether there is any way to use str2func within the Matlab function block to generate the outputs, x and y, based on the equations. I'm open to alternative solutions as well, perhaps something like procedurally generating named function handles in the workspace, and then calling those handles in the function block. Any help to achieve my goal would be appreciated.
I would eventually like the option of code generation, but it may not make financial sense depending on several factors. If code generation isn't feasible, I've considered using eval instead of str2func. I've read many other questions which all refer to eval as something to avoid at all costs, but it works in some of the tests I've tried (as long as I include coder.extrinsic('eval'). Since my equations are guaranteed to be boolean algebra, and all inputs and outputs belong to known lists, I think it eliminates some of the risk associated with eval. With that being the case, are there any other problems I need to worry about if I decide to go the eval route?
Edited for clarity, and removal of irrelevant passages after some of my previous strategies and thoughts proved impossible.

R2017a