【问题标题】:Error with print(): "Output argument "varargout" (and maybe others) not assigned"print() 出错:“未分配输出参数“varargout”(可能还有其他参数)”
【发布时间】:2012-05-17 11:15:56
【问题描述】:

我一直在尝试在 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='';

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

==> 柱状图在 30 打印时出错(hFig, '-dpng', strcat(filename, '.png'));

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

【问题讨论】:

  • 这可能不是它,但你在某个地方设置了hFig,对吧?
  • @mathematical.coffee 是的,之前

标签: matlab printing


【解决方案1】:

您的 if 代码与 {} 看起来很奇怪,在 MATLAB 中 {} 用于元胞数组和元胞数组索引,而不是用于代码结构。此外,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

【讨论】:

    【解决方案2】:

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

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

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

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

    [filename '.png'] 
    

    改为。

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

    附: :看到这个:Why do I get the error '??? Output argument ‹variable› (and maybe others) not assigned during call to ‹function›.' ?

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多