2016-02-08 72 views
0
function area = traparea(a,b,h) 
% traparea(a,b,h) Computes the area of a trapezoid given 
%     the dimensions a, b and h, where a and b 
%     are the lengths of the parallel sides and 
%     h is the distance between these sides 

% Compute the area, but suppress printing of the result 
area = 0.5*(a+b)*h; 

这只是一个例子。我想知道如何在单独的.m文件中声明值假设a = 5,b = 4,h = 8,并使用.in语句将它调用到原始函数(即traparea)中? 例如 。在1 = 5 像 请帮Matlab函数和输入

+0

不知道“.in声明”是什么。你只需从m文件调用函数:'area = traparea(5,4,8);'。 – TroyHaskin

回答

1

如果我明白了,你想创建一个脚本文件。创建一个名为“myscript.m”的文件名(选择你喜欢的任何名称),并将其放在与“traparea.m”相同的文件夹中。然后,在文件“myscript.m”,把下面的:

a = 5; 
b = 4; 
h = 8; 
result = traparea(a,b,h) % this is one way to show the result 
fprintf('my result is %f\n', result); % this is another way to display the result 

一旦你创建了两个文件“myscript.m”和“traparea.m”,您只需在输入“的MyScript”命令行。

+0

非常感谢你。非常有用的评论。如果你不介意,我应该再问一个问题吗?有没有什么办法可以像使用内部数据结构一样获得相同的结果? –