我试图编写一个代码,要求用户键入" heart"如果输入,则会出现心脏图。但我一直收到一个错误说"未定义的函数或变量' heart'"这就是我所拥有的。如果它自己运行,图表就可以正常运行,这样就不会出现问题了。谢谢!
result = input('Type "heart": ','s');
if (result == heart)
t = linspace(-pi,pi, 350);
X = t .* sin( pi * sin(t)./t);
Y = -abs(t) .* cos( pi * sin(t)./t);
plot(X,Y);
fill(X, Y, 'r');
set(gcf, 'Position', get(0,'Screensize'));
title('Happy Anniversary!', 'FontSize', 28);
end
答案 0 :(得分:0)
把心放在引号内:
result = input('Type "heart": ','s');
if (result == "heart")
t = linspace(-pi,pi, 350);
X = t .* sin( pi * sin(t)./t);
Y = -abs(t) .* cos( pi * sin(t)./t);
plot(X,Y);
fill(X, Y, 'r');
set(gcf, 'Position', get(0,'Screensize'));
title('Happy Anniversary!', 'FontSize', 28);
end
答案 1 :(得分:0)
您应该使用strcmpi,假设您需要不区分大小写的比较
result = input('Type "heart": ','s');
if (strcmpi(result,'heart'))
t = linspace(-pi,pi, 350);
X = t .* sin( pi * sin(t)./t);
Y = -abs(t) .* cos( pi * sin(t)./t);
plot(X,Y);
fill(X, Y, 'r');
set(gcf, 'Position', get(0,'Screensize'));
title('Happy Anniversary!', 'FontSize', 28);
end