【问题标题】:Concatenating strings while using figure command (MATLAB) [closed]使用图形命令(MATLAB)时连接字符串[关闭]
【发布时间】:2016-03-25 19:06:55
【问题描述】:

我正在尝试连接字符串,当我像这样单独连接它们时:

strcat({'Plot of f with a plot of iterates for c='},{int2str(c)})

没有错误发生。

但是当我尝试在这样的图形命令中使用它们时:

 figure('Name',strcat({'Plot of f with a plot of iterates for c='},{int2str(c)}))

我收到此错误:

Error using figure
Value must be a string

有什么原因吗?

【问题讨论】:

  • 您需要了解您的数据类型。 strcat 这里的输出是一个单元格,因为您正在传递它的单元格。单元格不是字符串。这里没有理由将单元格传递给strcat
  • 正如@excaza 所说。也不需要strcat,你可以隐式使用连接:str = ['Plot of f with a plot of iterates for c=', int2str(c)]

标签: string matlab concatenation matlab-figure string-concatenation


【解决方案1】:

正如@Matthias W 所指出的那样。strcat({'Plot of f with a plot of iterates for c='},{int2str(c)}) 的输出是1x1 cell,而不是figure() 函数所期望的字符串。

试试下面的

figure('Name',['Plot of f with a plot of iterates for c=', int2str(c)])

【讨论】:

    【解决方案2】:

    使用 [] 进行字符串连接是一个很好的简短解决方案。由于您已经在进行转换,您还可以考虑学习sprintf

    figure('Name', sprintf('Plot of f with a plot of iterates for c = %d.\n', c));
    

    在此示例中这可能有点过头了,但如果您需要打印出多个值,或者想要更好地控制数字的显示方式,这可能是值得的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2021-11-06
      • 2015-09-22
      • 2012-05-17
      • 2015-12-07
      • 2020-12-18
      • 2023-03-13
      相关资源
      最近更新 更多