필터 지우기
필터 지우기

Help to avoid the eval() function

조회 수: 2 (최근 30일)
Yunhui Zhou
Yunhui Zhou 2016년 11월 5일
편집: John D'Errico 2016년 11월 5일
I currently have problem using the feval() function to replace the eval() function.
The user will input a string array "function_name", which is the name of the function_name.m file; and a cell array "varargin", in which each cell is an input argument for the function_name.m file. The number of cells in "varargin" can't be determined.
Currently I'm using a for-loop to generate a string "function_name(varargin{1},varargin{2},...)", and then using the eval() function to execute the string, and it works. However, I wish to replace the eval() function with feval() function, but it seems hard to transfer all the input arguments. Is it possible to do the replacement?

답변 (3개)

KSSV
KSSV 2016년 11월 5일

Guillaume
Guillaume 2016년 11월 5일
feval(function_name, varargin{:})
Will call the function with however many arguments are stored in varargin.

John D'Errico
John D'Errico 2016년 11월 5일
편집: John D'Errico 2016년 11월 5일
Easy, peasy:
y = feval(fun,varargin{:});
The {:} makes a cell array (i.e., varargin) into what is called a comma separated list,exactly what feval needs. This is how you expand a cell array into an argument list.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by