cellfun changes in R2017b

3 visualizaciones (últimos 30 días)
Nathan Calvert
Nathan Calvert el 27 de Jul. de 2018
Comentada: Steven Lord el 27 de Jul. de 2018
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?

Respuestas (1)

Philip Borghesani
Philip Borghesani el 27 de Jul. de 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 comentario
Steven Lord
Steven Lord el 27 de Jul. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by