MATLAB Answers

cellfun changes in R2017b

조회 수: 2(최근 30일)
Nathan Calvert
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{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?

답변(1개)

Philip Borghesani
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:
  • 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
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.

댓글을 달려면 로그인하십시오.

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by