Calling a nested function from a dynamic regular expression throws an error
Show older comments
function test()
regexp('123456','\d(?@subfun($&))')
end
function subfun(s) %#ok<DEFNU>
disp(s)
end
and prints the expected sequence in the command window:
>> test
1
2
3
4
5
6
ans =
1 2 3 4 5 6
function test()
function subfun(s) %#ok<DEFNU>
disp(s)
end
regexp('123456','\d(?@subfun($&))');
end
throws this error:
Error using regexp
Attempt to add "matched" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in test (line 5)
regexp('123456','\d(?@subfun($&))');
Some experimentation showed that "matched" refers to the '&$' matched string token. I hypothesize that the "static workspace" referred to is somehow internal to regexp.
The error message's hyperlink goes to a page "Variables in Nested and Anonymous Functions", which states "If you attempt to dynamically add a variable to the workspace of an anonymous function, a nested function, or a function that contains a nested function, then MATLAB® issues an error of the form Attempt to add variable to a static workspace."
Given that there are no allocations within the nested function, why does this error occur? How is it possible to call a nested function (with dynamic input arguments) from a dynamic regular expression? I am using MATLAB R2012b.
Accepted Answer
More Answers (1)
Categories
Find more on Variables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!