如何在MATLAB中获得两个输出变量?

时间:2016-02-19 11:03:52

标签: matlab function output

function [ surface_area, volume ] = cube_and_sphere_calculator( geometry,l_r )
if geometry == 1
   surface_area = 6 * l_r^2
   volume = l_r^3 
elseif geometry == 0
   surface_area = 4*pi*l_r^3
   volume = (4/3)*pi*l_r^3
else
   disp('you have to choose a value that is either equal to one if you want to calculate the surface area and the volume of a cube or equal to zero if you want to calculate the surface area and the volume of a sphere')
end
end

如果我在公式后面加分号,matlab只给出一个输出变量:ans。这个答案等于表面积。我想要两个输出变量,我希望它们是surface_area和volume。为什么它不起作用?

1 个答案:

答案 0 :(得分:0)

来自matlab文档Declare function

function [m,s] = stat(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end

并调用该函数。

values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat(values)

所以你调用函数,就像你编写函数一样。