How to set function arguments to execute different set of m-files?

半世苍凉 提交于 2019-12-12 03:28:21

问题


I am using Matlab.

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}

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?

Thanks in advance


回答1:


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.

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


来源:https://stackoverflow.com/questions/31648737/how-to-set-function-arguments-to-execute-different-set-of-m-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!