Matlab在轴上绘制美元符号 [英] matlab plot dollar symbol on axis
问题描述
我希望以美元($)表示轴上的刻度标记.
I'd like the tick-mark labels on an axis to be represented in terms of dollars ($).
我尝试过:
%set(gca, 'ZTickLabel', sprintf('$%d|', get(gca,'ZTickLabel')));
当您旋转图形时,这种方法不起作用(这会拉伸图形并添加更多的刻度线.
This doesn't work when you rotate the graph (which stretches the graph and adds more tickmarks.
我还尝试使用cur2str
将其转换为货币,但这也不起作用.
I've also tried converting it to a currency using cur2str
, but that doesn't work either.
请帮助!
推荐答案
出现未定义行为的原因是因为XTickLabel
是一个字符数组:
The reason you are getting undefined behaviour is because XTickLabel
is a character array:
>> labels = get(gca,'XTickLabel');
>> class(labels)
ans =
char
当将其传递给sprintf
的%d
格式说明符时,它期望使用一个十进制数字-而不是字符数组-因此它将字符转换为整数表示并打印出来.
When you pass this to sprintf
's %d
format specifier, it's expecting a single decimal number - not an array of characters - so it converts the characters to their integer representations and prints those.
要查看此内容,请尝试:
To see this, try:
>> sprintf('%d','1')
ans =
49
您真正想做的是将字符数组视为字符数组:
What you really want to do is treat the character arrays like character arrays:
>> labels = get(gca,'XTickLabel')
labels =
0
1
2
3
4
5
6
7
>> dollar_signs = repmat('$',size(labels,1),1)
dollar_signs =
$
$
$
$
$
$
$
$
>> new_labels = [dollar_signs labels]
new_labels =
$0
$1
$2
$3
$4
$5
$6
$7
>> set(gca,'XTickLabels',new_labels)
这具有您想要的效果:
这篇关于Matlab在轴上绘制美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!