如何设置函数参数以执行不同的m文件集? [英] How to set function arguments to execute different set of m-files?
问题描述
我正在使用Matlab.
I am using Matlab.
我有一个主要功能main.m
.我有两组m文件,分别为:
I have a main function main.m
. And I have two sets of m-files, named:
Set A = {Area_triangle.m, Perimeter_triangle.m}
Set B = {Area_square.m, Perimeter_square.m}
是否有任何方法可以实现main(triangle)
可以执行A组中的m个文件,而main(square)
可以执行set B中的m个文件?
Is there any methods such that it can achieve main(triangle)
can execute m-files in set A while main(square)
can execute m-files in set B?
预先感谢
推荐答案
要运行存储在m文件中的Matlab脚本,可以使用run
.使用开关语句,很容易确定应使用哪个设置.然后,我们可以遍历给定集中的所有文件并执行脚本.
To run a Matlab-script stored in an m-file, you can use run
. With a switch-statement, it's easy to determine which set should be used. Then we can iterate over all the files in the given set and execute the scripts.
可以使用main('triangle')
和main('square')
调用以下函数:
The following function can be called with main('triangle')
and main('square')
:
function main(shape)
A = {'Area_triangle.m', 'Perimeter_triangle.m'};
B = {'Area_square.m', 'Perimeter_square.m'};
switch shape
case 'triangle'
S = A;
case 'square'
S = B;
otherwise
error('Shape not defined!');
end
for i = 1:length(S)
run(S{i})
end
这篇关于如何设置函数参数以执行不同的m文件集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!