Matlab-无法正确打印My Code的输出? -

时间:2017-11-06 02:32:28

标签: matlab

我在完成练习时输入了以下代码。输出是由空速,角度和方向组成的3个矢量。空速和角度是数字形式,方向是字符形式。

windspeed=[1 3];
groundspeed=[5 3];

north = [0,1];
south = [0,-1];
east = [1,0];
west = [-1,0];
airspeed = 0;
directionans = 0;
angle = 360;
% Begin your code after this line
vertical=[0 1];

airspeed=groundspeed-windspeed;

if (airspeed(1)>0 && airspeed(2)>0)
   direction='NE';
elseif all(airspeed==airspeed.*north)
    direction='N';
elseif (airspeed(1)<0 && airspeed>0)
    direction='NW';
elseif all(airspeed==airspeed.*south)
    direction='S';
elseif (airspeed(1)>0 && airspeed(2)<0)
    direction='SE';
elseif (airspeed(1)<0 && airspeed(2)<0)
    direction='SW';
elseif all(airspeed==airspeed.*west)
    direction='W';
elseif all(airspeed==airspeed.*east)
    direction='E';
end

if airspeed(2)<0
    vertical=south;
elseif airspeed(2)>0
    vertical=north;
end
vertical;

angle=acosd(dot(airspeed,vertical)/(norm(airspeed)*norm(vertical)));
[norm(airspeed) direction angle]

我想要一个

形式的输出
[50 'NW' 80]

相反,结果是

'EZ'

这可能是由于错误地将字符分配给变量。可以做些什么?

1 个答案:

答案 0 :(得分:1)

您不能将数字放入字符数组,或将字符放入数字数组中。要混合数据类型,您需要使用cell数组,这是通过大括号实现的:

>> {norm(airspeed) direction angle}
ans =
  1×3 cell array
    [50]    'NW'    [80]

我希望[50 'NW' 80][char(50) 'NW' char(80)]相同,等于2NWP