0

我一直在尝试在 MATLAB 中编写一个简单的脚本来绘制图像,询问用户是否要将其打印到文件中,并且(如果是)执行此操作。但是,我遇到了一个奇怪的print()函数错误。这是我的代码:

plot(X, Y, 'red');

choice = input('Do you want to print to file this 2D image ? [y/n] ', 'y');

if(choice=='Y' || choice=='y')
{
    print(hFig, '-dpng', strcat(filename, '.png'));
}

if如果正在运行,它会在语句中停止并出现错误:

==> 打印错误 161 err.message='';

???在调用“C:\Programmi\MATLAB\R2010a\toolbox\matlab\graphics\print.m>print”期间未分配输出参数“varargout”(可能还有其他参数)。

==> 直方图在 30 print(hFig, '-dpng', strcat(filename, '.png'));

为什么会出现此错误,如何避免此错误?

4

2 回答 2

4

您的if代码在 MATLAB 中看起来很奇怪,{用于元胞数组和元胞数组索引,而不是用于代码结构。此外,must的第二个参数不是你所拥有的。}{}input's''y'

固定代码:

choice = input('Do you want to print to file this 2D image ? [y/n] ', 's');

if(choice=='Y' || choice=='y')
    print(hFig, '-dpng', strcat(filename, '.png'));
end

编辑:一直询问直到用户用“y”、“Y”、“n”或“N”回应:

choice = '';
while ~ismember(choice, {'y', 'Y', 'n', 'N'})
    choice = input('Do you want to print to file this 2D image ? [y/n] ', 's');
end

if(choice=='Y' || choice=='y')
    print(hFig, '-dpng', strcat(filename, '.png'));
end
于 2012-05-17T12:04:35.973 回答
0

太混乱了!因为打印命令没有任何输出参数!!!

我不清楚,但我认为首先检查 hFig 分配。您可以使用

hFig=figure;
plot(X, Y, 'red');
% ...

如果你想创建一个图形并在其中绘制任何你想要的东西。由于错误表明您的输出参数未分配,因此请检查“文件名”或者您可以使用

[filename '.png'] 

反而。

我希望这对你有帮助。现在我没有 MATLAB,我无法为你测试它。

PS:看到这个:为什么我得到错误'??? 在调用‹function›期间未分配输出参数‹variable›(可能还有其他)。?

于 2012-05-17T11:46:19.457 回答