Matlab函数参数包括h。我希望情节标题包含相同的h。怎么做?

问题描述 投票:-1回答:4

函数参数包括h(= 10)。我希望情节标题包含相同的h。怎么做?

function G=graphit(X,Y,ye,h)
plot(X,Y,'-'); 
grid
title([ 'Approximate and Exact Solution @h= .', num2str(h)])

谢谢。 MM

matlab plot title figure
4个回答
0
投票

您可以使用sprintf创建格式化字符串

title( sprintf( 'Approximate and Exact Solution. h = %.0f', h ) );

0
投票
title(['Approximate and Exact Solution ',num2str(h),' .'])

0
投票
title_string = sprintf('Approximate and Exact Solution @h= %d.',h) % change d to f for floats
title(title_string)

我使用正确的字符串格式化工具,如sprintf来构建格式正确的标题。


0
投票

尽管在不到5分钟的时间内给出了3个优秀的答案,但没有一个建议的代码可以正常运行。基本上,我得到了与运行原始代码时几乎相同的结果。

事实证明,对于h的数字中的前导零(例如01或05)将导致系统降低零。这对我来说是一个问题,因为我希望h值为.05,.025,.01。此外,Matlab软件似乎与指定的小数点混淆,后跟带有前导零的数字。解决这个问题的方法是用小数点(.10,.05,.025,。01)传递小数点。见下面的代码。

输入是

X,Y,xe,ye,.01

工作代码:

function G=graphit(X,Y,xe,ye,h)
hold on; 
plot(X,Y,'-'); plot(X,ye,'-.'); 
hold off
title([ 'Approximate and Exact Solution @h=', num2str(h)])

预期和获得的输出:近似和精确解@ h = 0.01

瞧!谢谢你的回复......

© www.soinside.com 2019 - 2024. All rights reserved.