- Use only explicit variables when constructing anonymous functions. If an anonymous function accesses any variable or nested function that is not explicitly referenced in the argument list or body, MATLAB throws an error when you invoke the function. Implicit variables and function calls are often encountered in the functions such as eval, evalin, assignin, and load. Avoid using these functions in the body of anonymous functions.
cellfun changes in R2017b
1 view (last 30 days)
Show older comments
I am trying to use cellfun in the following context but it's giving an error "Undefined variable it or class (it)":
% Convert struct of interp_tables to cell array if isstruct(it) it = struct2cell(it); end
% Process filename pattern
tokens = regexp(pattern,'<([^>]+)>','match');
etokens = strrep( tokens,'$i','i');
etokens = strrep(etokens,'$v','$v1');
etokens = strrep(etokens,'$d','$d1');
etokens = regexprep(etokens,'\$v(\d+)','it{i}.variable_names{$1}');
etokens = regexprep(etokens,'\$d(\d+)','it{i}.dimension_names{$1}');
% Write to file
for i = 1:numel(it)
token_vals = cellfun(@(x) num2str(eval(x(2:end-1))),etokens,'uniform',false);
file = strrep(pattern,tokens,token_vals);
it{i}.to_file(file{1},varargin{:});
end
It works in the previous versions of Matlab but changes to the built-in cellfun break it here. Is there a work-around?
0 Comments
Answers (1)
Philip Borghesani
on 27 Jul 2018
There was no change to cellfun. You are (were?) misusing eval inside of an anonymous function, In R2015b and later MATLAB will not let you get away with it. Copied directly from the help page:
There are tricks that will allow it to be seen in the eval but you are much better off re-coding this in a way that does not use eval.
1 Comment
Steven Lord
on 27 Jul 2018
FYI, in addition to that information being on the help page we announced it in the Release Notes when we made this change in release R2015b.
See Also
Categories
Find more on Whos 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!