Edd*_*heB 2 matlab class matlab-figure
我必须在这里找到一些非常简单的东西.我有一个MATLAB类,它创建一个包含两个按钮的图形,每个按钮调用相同的功能,但这些按钮的句柄不保留在该函数中,我不知道为什么.
这是班级,简化......
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
Run Code Online (Sandbox Code Playgroud)
如果我调用它会打开图形,并显示按钮的句柄编号:
>> T = Test
T =
Test with properties:
Figure: 8
ButtonA: 745.000122070313
ButtonB: 103.002319335938
>> T.ButtonA
ans = 745.000122070313
>> T.ButtonB
ans = 103.002319335938
Run Code Online (Sandbox Code Playgroud)
如果按下按钮A,该函数将返回两个空值,就好像尚未设置属性一样:
Button =
745.000122070313
ans =
[]
ans =
[]
Run Code Online (Sandbox Code Playgroud)
如果按下按钮B,该函数会返回按钮A的值,但按钮B的值为空值:
Button =
103.002319335938
ans =
745.000122070313
ans =
[]
Run Code Online (Sandbox Code Playgroud)
我会非常感谢任何建议.
你应该从句子中继承你的类.检查MATLAB的之间的区别value-class和handle-Class 这里.
为此,请更改代码中的第一行:
classdef Test < handle
Run Code Online (Sandbox Code Playgroud)