- 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 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Philip Borghesani
2018-7-27
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 个评论
Steven Lord
2018-7-27
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!