Question

How to get the value of a uicomponent slider on Matlab when a key is pressed? I'm using this in the GUI code:

% --- 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)

The slider works correctly, but when I press a key, this returns the error "Undefined variable "handles" or class "handles.slid.value". How to solve?

Was it helpful?

Solution

You can connect a callback to the desired event and get the value there from the src parameter

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

If the sliderCallback function is not accessible globally, you can instead set it through function pointer:

set(handles.slid, 'KeyPressedCallback',@sliderCallback);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top