0

按下键时如何在Matlab上获取uicomponent滑块的值?我在 GUI 代码中使用它:

% --- Executes just before teste is made visible.
function teste_OpeningFcn(hObject, eventdata, handles, varargin)

% Choose default command line output for teste
handles.output = hObject;
handles.slid=uicomponent('style','slider',0,9,5,'position',[10 30 200 50]);

set(handles.slid, 'KeyPressedCallback','handles.slid.value');

% Update handles structure
guidata(hObject, handles)

滑块工作正常,但是当我按下一个键时,这会返回错误"Undefined variable "handles" or class "handles.slid.value"。如何解决?

4

1 回答 1

1

您可以将回调连接到所需事件并从src参数中获取值

handles.slid=uicomponent('style','slider',0,9,5,'position',[10 30 200 50]);
set(handles.slid, 'KeyPressedCallback','sliderCallback');

function sliderCallback(src,evt)
    display(['slider state ' num2str(get(src, 'Value'))]);
end

如果sliderCallback函数不能全局访问,你可以通过函数指针来设置它:

set(handles.slid, 'KeyPressedCallback',@sliderCallback);
于 2012-09-12T05:26:27.083 回答