MATLAB Answers


cellfun changes in R2017b

Nathan Calvert さんによって質問されました 2018 年 7 月 27 日
最新アクティビティ Steven Lord
さんによって コメントされました 2018 年 7 月 27 日
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 works in the previous versions of Matlab but changes to the built-in cellfun break it here. Is there a work-around?

  0 件のコメント

サインイン to comment.





1 件の回答

Answer by 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:
  • 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.
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
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.

サインイン to comment.

Translated by