我有3个文件:1.驱动程序。m在文件Perimeter中调用函数Perimeter()。m并调用文件area.m.中的函数Area()2.两个函数文件:Perimeter。m和arem
现在我想结合周长。m和Area。m放入一个文件中,例如myFunction。m包含两个函数Perimeter()和Area()。这在Matlab中可能吗?
function A = Area(x)
A = pi* x^2;
end
function P = Perimeter(x)
P = 2*pi*x;
end
% Calculate area and perimeter of circle x
x = 5;
perimeter = Perimeter(x)
area = Area(x)
谢谢你的帮助
答案是肯定的。最简单的方法是使用所谓的嵌套函数
function [perimeter,area] = main()
function A = Area(x)
A = pi* x^2;
end
function P = Perimeter(x)
P = 2*pi*x;
end
% Calculate area and perimeter of circle x
x = 5;
perimeter = Perimeter(x);
area = Area(x);
end