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

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

Steven Lord
Steven Lord il 29 Set 2022
Let's look at the first part of your text.
@(X,Y,var)8,var(65),var(57)0,var(57)1,var(57)2,var(57)3,var(57)4,var(57)5)(var(57)5.*
Your anonymous function is:
@(X,Y,var)8
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.)
var(65)
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.
var(57)0
Your string repeats this pattern several times then has a right parenthesis without a corresponding left parenthesis.
var(57)5)
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.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by