How can I determine a function in a folder and call it programmatically in MATLAB?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a folder which contains several functions. I want to obtain a list of all the functions in this folder and call them with appropriate input arguments.
채택된 답변
MathWorks Support Team
2010년 1월 21일
In the code below demonstrates reading all the files with a .m extension from a folder, identifying the function name 'plot' from the list and then calling it using FEVAL to plot a line from 1 to 10.
% Find all the files with the .m extension in a directory
graph_files = dir(fullfile(matlabroot,'\toolbox\matlab\graph2d\*.m'));
% Get the names of the files into a cell array
g_f = cell(length(graph_files),1);
[g_f{:}] = deal(graph_files.name);
% Identify the file to be used
x = strmatch('plot.m', g_f, 'exact');
% Remove .m from the end
name = g_f{x}(1:end-2);
% Use the function with appropriate input arguments
feval(name,1:10);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!