【问题标题】:Matlab error : ??? Subscript indices must either be real positive integers or logicalsMatlab错误:???下标索引必须是实数正整数或逻辑数
【发布时间】:2013-04-19 16:58:17
【问题描述】:

我自己编写的函数有问题。我没有找到任何问题如何自己解决,所以我在这里发布它希望有人会看到我没有看到的东西......(这是我的第一个问题,我不知道如何格式化它matlab代码,不好意思)

这是我的函数,它计算光线(由段 CD 给出)在“墙”(由 AB 给出)上的入射角。 AB(墙)应该是水平或垂直的,段 CD 和墙 AB 应该有一个交叉点:

function angle = incidentAngle(AB,CD)

%AB and CD are two vectors of size 1x4
%AB = [a1 a2 b1 b2] et CD = [c1 c2 d1 d2]
%AB is a segment from the point (a1,b1) to the point (a2,b2)


    if(AB(1)==AB(3)) % The wall is vertical
        if(CD(1)==CD(3)) % The "ray" is vertical too, the angle is 90° or pi/2
            angle = pi/2;
        else
            angle = abs(atan((CD(2)-CD(4))/(CD(1)-CD(3))));
        end
    else
        if(AB(2)==AB(4))% The wall is horizontal
            if(CD(2)==CD(4)) % The "ray" is horizontal too, the angle is 90° or pi/2
                angle = pi/2;
            else
                angle = abs(atan((CD(1)-CD(3))/(CD(2)-CD(4))));
            end
        end
    end

end

问题(例如)是:

当我将 AB 和 CD 定义为

AB = [0 1 0 5];
CD = [-1 2 3 4];

我会这样做的

angle = incidentAngle(AB,CD)

在命令窗口中,我收到消息错误:

??? Subscript indices must either be real positive integers or logicals.

我不明白为什么……

如果现在我只是复制命令窗口中的函数,像这样:

AB = [0 1 0 5];
CD = [-1 2 3 4];

angle = 0;

if(AB(1)==AB(3)) % The wall is vertical
    if(CD(1)==CD(3)) % The "ray" is vertical too, the angle is 90° or pi/2
        angle = pi/2;
    else
        angle = abs(atan((CD(2)-CD(4))/(CD(1)-CD(3))));
    end
else
    if(AB(2)==AB(4))% The wall is horizontal
        if(CD(2)==CD(4)) % The "ray" is horizontal too, the angle is 90° or pi/2
            angle = pi/2;
        else
            angle = abs(atan((CD(1)-CD(3))/(CD(2)-CD(4))));
        end
    end
end

angle

我得到了正确的答案,

angle =

0.4636

【问题讨论】:

  • 我在我的工作区中创建了一个新功能,并尝试使用您的输入重新创建您的错误,它对我来说效果很好。不太确定是什么问题。我正在使用 R2012b 顺便说一句
  • 我不明白的是为什么它可以正常工作,但在我调用函数时却不行......该函数(我认为)写得很好,并且该函数保存在正确的文件夹中,虽然它只是不要跑...

标签: matlab function


【解决方案1】:

有一个名为angle 的内置Matlab 函数,用你的变量名覆盖它会带来麻烦。尝试更改该变量名称,改用名称angl。这应该可以解决您的问题。

【讨论】:

  • @mwoua 您是否也更改了函数的名称?
  • 当我使用 function angl = incidentAngle(AB,CD) 并将所有出现的 angle 变量更改为 angl 时,它对我有用
  • 当我将所有“角度”更改为“角度”时,它没有工作......但是当我将函数名称更改为“事件角度”时,它工作了。谢谢!
  • 最后一件事,你如何突出显示(这是一个正确的词吗?我是比利时人,不知道正确的词......无论如何)像“function angl = eventAngle(AB ,CD)”在您的评论中?
  • 在要突出显示的单词前后使用`符号。
猜你喜欢
  • 2012-12-29
  • 2014-09-25
  • 2012-11-27
  • 2013-12-04
  • 2014-02-02
  • 2021-07-02
  • 2012-04-14
相关资源
最近更新 更多