UIAxes的YLim属性无法收听 [英] UIAxes' YLim property cannot be listened to

查看:215
本文介绍了UIAxes的YLim属性无法收听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB提供了 addlistener 函数. /p>

侦听器使我们能够跟踪对象属性的更改并对其进行操作.例如,我们可以创建一个非常简单的侦听器,当'YLim'属性时,该侦听器将在命令窗口中显示一条消息. html"rel =" nofollow noreferrer> axes 对象已更改:

% Example using axes
ax = axes();
addlistener(ax, 'YLim', 'PostSet', @(src, evnt)disp('YLim changed'));

尝试平移轴或放大/缩小,看看会发生什么.效果很好.

我需要这样做,但要使用 uiaxes .

不幸的是,看来我们不允许这样做.尝试运行以下示例:

% Example using uiaxes
ax = uiaxes();
addlistener(ax, 'YLim', 'PostSet', @(src, evnt)disp('YLim changed'));

它抛出此错误:

使用matlab.ui.control.UIAxes/addlistener时出错 PostSet侦听器,类"matlab.ui.control.UIAxes"中的属性"YLim" 未定义为SetObservable.

我已阅读文章收听属性值的更改 App Designer .

解决方案

我们应该将侦听器附加到内部Axes对象,而不是UIAxes本身.试试这个:

hFig = uifigure();
hAx = uiaxes(hFig);
addlistener(struct(hAx).Axes, 'YLim', 'PostSet', @(src, evnt)disp("YLim changed"));
hAx.YLim = [0 2];

万一有人想知道,我是通过反复试验发现的.

在R2018a&上测试R2018b.

MATLAB provides the addlistener function.

Listeners allow us to keep track of changes of an object's properties and act upon them. For example, we can create a very simple listener that will display a message in the command window when the 'YLim' property of an axes object is changed:

% Example using axes
ax = axes();
addlistener(ax, 'YLim', 'PostSet', @(src, evnt)disp('YLim changed'));

Try panning the axes or zooming in/out and see what happens. This works fine.

I need to do the same but using an uiaxes instead.

Unfortunately, it looks like we are not allowed to do so. Try running the following example:

% Example using uiaxes
ax = uiaxes();
addlistener(ax, 'YLim', 'PostSet', @(src, evnt)disp('YLim changed'));

It throws this error:

Error using matlab.ui.control.UIAxes/addlistener While adding a PostSet listener, property 'YLim' in class 'matlab.ui.control.UIAxes' is not defined to be SetObservable.

I've read the articles Listen for Changes to Property Values and Observe Changes to Property Values and I learned that a property must be declared as SetObservable to allow being listened:

classdef PropLis < handle
   properties (SetObservable)
      ObservedProp = 1 % <-- Observable Property
   end
end

I've tried taking a look at the UIAxes class definition via edit matlab.ui.control.UIAxes but it's not possible because it's a P-file.

If 'YLim' is not observable then how can I keep track of changes in this property?

I'm using App Designer in MATLAB R2018b.

解决方案

We should attach the listener to the internal Axes object, and not the UIAxes itself. Try this:

hFig = uifigure();
hAx = uiaxes(hFig);
addlistener(struct(hAx).Axes, 'YLim', 'PostSet', @(src, evnt)disp("YLim changed"));
hAx.YLim = [0 2];

In case anybody is wondering, I found this via trial and error.

Tested on R2018a & R2018b.

这篇关于UIAxes的YLim属性无法收听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆