How to Have Non-Zero Symbol for Incomplete Labels

2019-01-29 00:13发布

I run into the problem where Matlab 2015b expands the labels of new Xticks when the x-axis gets bigger by using incomplete label, zeros, in the thread No Gap Next to Axis Label in Matlab?

enter image description here

The dynamic expansion of incomplete labels of xticks is not possible because there is always cases of insufficient space but only one symbol is needed to mark half between two values. The situation is problematic with zeros because I have several calibration points and several systems where the extra zeros are errorprone. I would like to have there another symbol.

Example code how to create those incomplete labels of 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!

Without those incomplete labels of xticks but broader labelling which is worser

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);

Output of Suever's answer

Beautiful Small window in the original size with scientific numbering because of callback(); at the end of the code following

enter image description here

Medium window

enter image description here

Code

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

How can you have another symbol for the incomplete labels of xticks in Matlab?

1条回答
别忘想泡老子
2楼-- · 2019-01-29 01:13

As I said in the other question, if you want the labels to be updated automatically when you resize things, you'll want to do the following.

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();

As you change the size of your figure, the labels will be changed automatically and the positions will be updated.

Small Window

enter image description here

Medium Window

enter image description here

Large Window

enter image description here

Note: Test this code in isolation to verify that it works, then adapt the idea to your solution. It seems like you're ending up with a lot of complications because your namespace is polluted (for example your examples don't even run because labels isn't defined).

查看更多
登录 后发表回答