使用图形命令连接字符串 (MATLAB)
Concatenating strings while using figure command (MATLAB)
我正在尝试连接字符串,当我像这样分别连接它们时:
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
有什么原因吗?
正如@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)])
使用 []s 进行字符串连接是一个很好的简短解决方案。由于您已经在进行转换,您还可以考虑学习 sprintf:
figure('Name', sprintf('Plot of f with a plot of iterates for c = %d.\n', c));
在此示例中,这可能有些矫枉过正,但如果您需要打印出多个值,或者希望更好地控制数字的显示方式,那么这样做可能是值得的。
我正在尝试连接字符串,当我像这样分别连接它们时:
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
有什么原因吗?
正如@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)])
使用 []s 进行字符串连接是一个很好的简短解决方案。由于您已经在进行转换,您还可以考虑学习 sprintf:
figure('Name', sprintf('Plot of f with a plot of iterates for c = %d.\n', c));
在此示例中,这可能有些矫枉过正,但如果您需要打印出多个值,或者希望更好地控制数字的显示方式,那么这样做可能是值得的。