为什么这个MATLAB类没有保留其属性? [英] Why isn't this MATLAB class holding on to its properties?
问题描述
在这里一定缺少一些非常简单的东西.我有一个MATLAB类,该类创建一个包含两个按钮的图形,每个按钮调用相同的函数,但是这些按钮的句柄未保留在该函数中,我也不知道为什么.
There must be something very simple that I'm missing here. I have a MATLAB class which creates a figure containing two push-buttons, each button calls the same function, but the handles for those buttons aren't retained within that function, and I don't know why.
这是课程,简化了...
Here's the class, simplified...
classdef Test
properties
Figure
ButtonA
ButtonB
end
methods
function app = Test()
app.Figure = figure();
app.ButtonA = uicontrol('Style', 'pushbutton', ...
'String', 'Button A', ...
'Position', [10, 10, 100, 20], ...
'Callback', @app.PressButton);
app.ButtonB = uicontrol('Style', 'pushbutton', ...
'String', 'Button B', ...
'Position', [10, 120, 100, 20], ...
'Callback', @app.PressButton);
end
function PressButton(app, Button, ~)
Button
app.ButtonA
app.ButtonB
end
end
end
如果我叫它,它将打开图形,并显示按钮的手柄编号:
If I call it it opens the figure, and displays the handle numbers for the buttons:
>> T = Test
T =
Test with properties:
Figure: 8
ButtonA: 745.000122070313
ButtonB: 103.002319335938
>> T.ButtonA
ans = 745.000122070313
>> T.ButtonB
ans = 103.002319335938
如果按下按钮A,该函数将返回两个空值,就像尚未设置属性一样:
If I press Button A, the function returns two empty values, as if the properties have not been set:
Button =
745.000122070313
ans =
[]
ans =
[]
如果我按下按钮B,则该函数的确会返回按钮A的值,但会返回按钮B的空值:
If I press Button B, the function does return the value for button A, but an empty value for button B:
Button =
103.002319335938
ans =
745.000122070313
ans =
[]
非常感谢您提出任何建议.
I'd be very grateful for any suggestions.
推荐答案
您应该从handle继承您的类.
检查matlab的value
类和handle
类之间的区别这里.
You should subclass your class from handle.
Check the difference between matlab's value
-class and handle
-class here.
为此,请更改代码的第一行:
To do so, change the first line in your code:
classdef Test < handle
这篇关于为什么这个MATLAB类没有保留其属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!