Error when using str2func: "Invalid expression. Check for missing multiplication operator, missing [...]"

3 views (last 30 days)
Full error text: "Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error.
To construct matrices, use brackets instead of parentheses."
Short summary: I'm creating a target function I want to use in a fmicon optimization process. The target function is assembled iteratively from a set of 2-D data points. Everything works fine if the number of points is small, but after a certain threshold I get that error message.
I've attached the content of the function I want to transform in a text file (I exported the variable to a .txt file via writematrix). The line of code that gives me the error is:
Newfunc = str2func(tabledata)
What I'd like to know:
1) Is there a way to find at which point of my text file the str2func function crashes? It's too long for a human to just notice a tiny error.
2) Is the function too long for Matlab to handle? As I increase the number of iterations the function becomes longer, and I'm not sure where the limits are.
Thank you!

Accepted Answer

Steven Lord
Steven Lord on 29 Sep 2022
Let's look at the first part of your text.
Your anonymous function is:
Its definition ends at the comma. The next part of your string would display element 65 of the variable var (or call the var function with the scalar 65 as input.)
The next part of your string is not valid MATLAB code and would throw that error. You may have intended for it to be the product of var(57) and 0 but you forgot the multiplication operator.
Your string repeats this pattern several times then has a right parenthesis without a corresponding left parenthesis.
I don't know if the latter issue occurs later in the string as well, but the former issue is a recurring issue throughout the string. I'd take a long, hard look at the code that generated this string because it's not behaving correctly.
To be honest, IMO this anonymous function is going to be nigh-impossible to debug if anything goes wrong. Instead of having whatever code assembled this string creating something that you intend to turn into an anonymous function I'd use it to create a MATLAB function file where each term or collection of terms is either a separate variable or is added to the running total on a separate line. You can use file I/O functions like fopen, fprintf, and fclose to create that function file.
With a function file you could use the debugging tools in MATLAB to identify where errors occur rather than just being told "it's somewhere in this long string." Another tool to help you validate the generated code would be Code Analyzer: if you opened the file in the Editor you would have seen a LOT of red Code Analyzer messages and they'd direct your attention to the location with the missing operators.

More Answers (0)


Find more on Function Creation 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!