Matlab:引用一组m文件中的函数 [英] Matlab: reference to functions in a set of m-files
问题描述
我有一个包含n个m文件(cI_0001.m,cI_0002.m,...)的目录,用于定义每个应该用于图像转换的复杂函数,其转换形式为:
I have a directory with n m-files (cI_0001.m, cI_0002.m, ...) defining each a complex function that should be used for image transformation with imtransform the kind of:
conformal = maketform('custom', 2, 2, [], @cI_0001, []);
T = imtransform(C, conformal, ...);
在最简单的情况下,我想要一个循环,将imtransform应用于所有给定函数的图像C.所以我为没有扩展名的m文件名(m_name_list)创建了一个单元格数组,我想我需要在maketform()中使用一种指针来指定m_name_list中的元素。如何正确制定?
In the simplest case I want a loop that apply imtransform to an image C for all the given functions. So I made a cell array for the directory with the m-file names without extension (m_name_list) and I think I need a kind of a pointer in maketform() to the elements in m_name_list. How can this be correctly formulated?
for i=1:n
conformal = maketform('custom', 2, 2, [], @pointer_to_the_ith_element_in_m_name_list, []);
T = imtransform(C, conformal, ...);
imwrite(T, ...);
end
我看到提案在如何设置函数参数以执行不同的m文件集但这似乎不适用于调用maketform()的函数。
I saw the proposal with run in How to set function arguments to execute different set of m-files but this seems not working with the function calling in maketform().
编辑:我包含一个带有函数名称+扩展名的列表,可以在其中创建指针(可能扩展到路径列表+名称):
I include a list with function names + extension to which pointers could be made (possible extended to a list of paths+names):
m_dir_entries = dir(strcat(CM_inverse_folder_path, '*.m'));
m_name_list = cell(1,length(m_dir_entries));
for k=1:length(m_dir_entries)
m_name_list{k} = m_dir_entries(k).name;
end
推荐答案
你可以使用 str2func
创建一个每个.m文件的函数句柄
You can use str2func
to create a function handle to each of the .m files
% Add the folder to your path
addpath(CM_inverse_folder_path)
function_list = {'cI_0001.m', 'cI_0002.m'};
% Remove the extensions
[~, function_list] = cellfun(@fileparts, function_list, 'UniformOutput', false);
for k = 1:n
func = str2func(function_list{k});
conformal = maketform('custom', 2, 2, [], func, []);
T = imtransform(C, conformal, ...);
imwrite(T, ...);
end
% Remove the folder from the path
rmpath(CM_inverse_folder_path)
这篇关于Matlab:引用一组m文件中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!