如何在Matlab中使用XTicks不完整标签的非零符号?

时间:2016-03-03 18:04:30

标签: matlab calibration

我遇到了问题,Matlab 2015b通过在线程No Gap Next to Axis Label in Matlab?

中使用不完整的标签零来增加x轴变大的新Xticks的标签

enter image description here

xticks的不完整标签的动态扩展是不可能的,因为总是存在空间不足的情况,但是只需要一个符号来标记两个值之间的一半。 因为我有几个校准点和几个系统,其中额外的零是错误的,所以零的情况是有问题的。 我想有另一个符号。

示例代码如何创建xticks的不完整标签

labels = arrayfun(@(x)sprintf('%.2g', x), xticks, 'uniform', 0);
ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]); % anything here
xticks = get(ax2, 'xtick'); % https://stackoverflow.com/a/35776785/54964
set(ax2, 'xticklabels', labels); % here the point!

没有那些不完整的xticks标签,但更广泛的标签更糟糕

labels = arrayfun(@(x)sprintf('%.2g', x), xticks, 'uniform', 0);
ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
xticks = get(ax2, 'xtick'); % https://stackoverflow.com/a/35776785/54964
set(ax2, 'xtick', xticks, 'xticklabels', labels);

Suever answer

的输出

漂亮原始大小的小窗口,由于代码末尾的callback();而具有科学编号

enter image description here

中窗口

enter image description here

代码

hFig=figure;
data=randi(513,513);
D=mat2gray(pdist(data, 'correlation'));

ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
axis(ax2, 'square');
title('Corr pdist');
cbar2 = colorbar(); 
set(ax2, 'XLim', [0 size(D,2)]);
set(cbar2, 'Visible', 'off')
grid minor;
labelconverter = @(x)sprintf('%.2g', x); % https://stackoverflow.com/a/35780915/54964
callback = @(varargin)set(ax2, 'xticklabels', arrayfun(labelconverter, get(ax2, 'xtick'), 'uniform', 0));
set(hFig, 'SizeChangedFcn', callback);
callback(); % necessary for small window

如何在Matlab中为xticks的不完整标签设置另一个符号?

1 个答案:

答案 0 :(得分:3)

正如我在另一个问题中所说,如果您希望在调整大小时自动更新标签,您将要执行以下操作。

fig = figure;

% Set large xlimits to demonstrate the issue at hand
ax2 = axes('xlim', [0 1e9]);

% Force a draw event to have the axes determine where the
labelconverter = @(x)sprintf('%.2g', x);
callback = @(varargin)set(ax2, 'xticklabels', arrayfun(labelconverter, get(ax2, 'xtick'), 'uniform', 0));

set(fig, 'SizeChangedFcn', callback);

% Be sure to execute the callback to get new labels prior to figure resize.
callback();

当您更改图形的大小时,标签将自动更改,位置将会更新。

小窗口

enter image description here

中等窗口

enter image description here

大窗口

enter image description here

  

注意:单独测试此代码以验证其是否有效,然后根据您的解决方案调整该想法。由于您的命名空间受到污染(例如您的示例甚至无法运行,因为labels未定义),您似乎最终会遇到很多复杂问题。