寡妇调整大小时,matlab图形事件侦听器不会触发 [英] matlab figure event listener does not trigger when widow resized
问题描述
版本:Matlab R2014b
Version: Matlab R2014b
XY问题免责声明:我的目标是使用某些自定义函数在图形中制作MATLAB格式的刻度,该自定义函数将刻度位置作为参数并输出一个字符串.默认刻度位置可以,但是我想控制标签.由于滴答声变化而无法执行回调函数会导致标签错误的滴答声.
XY problem disclaimer: my objective is to make MATLAB format ticks in my graphic with a certain custom function that takes as argument the tick position and outputs a string. Default tick placement is ok but I want to control the labels. Failure to execute the callback function as ticks change results in mislabelled ticks.
下面的MWE显示了回调的虚拟版本;真正的回调将刻度标签设置为
The MWE below shows dummy versions of the callback; the real callback sets tick labels to the output of
tick_formatter = @(tv)([sprintf('%1.2f',tv/10^N) 'e' num2str(N)])
(其中tv是以X为单位的刻度值). (这是R2015b及以后版本的ax.XAxis.Exponent
的粗略替代,请参见 https://fr.mathworks.com/matlabcentral/answers/275054-change-multiple-exponent-on-x轴)
(where tv is the tick value in X units). (This is a crude replacement for ax.XAxis.Exponent
of versions R2015b and following, see https://fr.mathworks.com/matlabcentral/answers/275054-change-multiple-exponent-on-x-axis)
实际问题:
我希望每次滴答变化(即其数量或y值变化)时都能执行某些操作"(在我的情况下,重新计算刻度标签).为此,我查看了addlistener
,但我无法使其按预期工作.
I want to be able to do "something" (in my case recompute tick labels) every time ticks change (i.e. their number or their y-value changes). To do so I looked at addlistener
but I cannot manage to make it work as I wish.
理想情况下,我会在XTick
属性上放置一个侦听器,但它似乎无法正常工作.当XTickMode
置于auto
上时,放大和缩小(通过xlim()
或手动进行缩放)或调整图形窗口的大小将导致刻度的数量/位置发生变化,从而更改get(ax,'XTick')
返回的值,但不会触发此类事件侦听器.
Ideally, I would put a listener on the XTick
property but it does not seem to work correctly. When XTickMode
is on auto
, zooming in and out (programatically by xlim()
or by hand) or resizing the graphic window will cause change in the number / position of ticks, and consequently change the value returned by get(ax,'XTick')
, but it will not trigger such an event listener.
要放大/缩小,一种解决方法是改为监听XLim
,它会触发事件.但是我发现没有这样的变通方法来调整图形窗口的大小(至少在轴对象级别使用侦听器).
For zooming in/out, a workaround is to listen to XLim
instead, which does trigger events; but I found no such workaround for figure window resizing (at least using listeners at the axes object level).
欢迎提出任何建议.
MWE :
% generate figure
x = 0:100;
y = x.^2;
figure(1)
clf() % ensure we cleared out previous tests
plot(x,y)
ax = gca();
% This event listener does nothing as you zoom in/out or resize the graphic
% window, even though XTick does change; it will only respond to explicit
% commands e.g. set(ax, 'XTick', ...).
listen_to_XTick = addlistener(ax, 'XTick', 'PostSet',@(src,evn) disp('XTick changed'));
% This event listener responds to zooming in/out, but not to window
% resizing
listen_to_XLim = addlistener(ax, 'XLim', 'PostSet',@(src,evn) disp('XLim changed'));
% Play a bit with the figure, zoom in/out, resize the graphics window etc.
% -> only listen_to_XLim ever gets triggered, never listen_to_XTick
% Other properties that do not seem to ever trigger as window is resized:
% 'XTick', 'XTickLabel', 'Position'
推荐答案
If you would like to change your tick labels when your figure window is resized, you could make use of the SizeChangedFcn
callback of the figure window itself. Try:
f = figure('SizeChangedFcn',@(src,evn) disp('Window resized'))
这篇关于寡妇调整大小时,matlab图形事件侦听器不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!