【问题标题】:How do I get two output variables in MATLAB? [duplicate]如何在 MATLAB 中获取两个输出变量? [复制]
【发布时间】:2016-02-19 11:03:51
【问题描述】:
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。为什么它不起作用?

【问题讨论】:

    标签: matlab function output


    【解决方案1】:

    来自 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)
    

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

    【讨论】:

      猜你喜欢
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多