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

7 views (last 30 days)
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.

Answers (1)

Emerson Butler
Emerson Butler on 30 Apr 2019
This is really just a workaround that works for me, just posting in case someone in the future has a similar problem. Since I had my strings '"x = s || t" and "y = s && t", I decided to copy all of the entries in the equation string and paste them into Excel. I then copy them from Excel and paste them into my Matlab function block (Pasting directly from equation string to function block results in strings instead of regular text). This extra step needed by the user is better than having the user manually type the 200+ equations. Like I said, this is not a pure solution, just a workaround that works for me. I'm always looking to make my user's experience more streamlined (and less open to operator error such as not copying all of the values), so if anyone can think of a real solution please post it here.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!