How to store slider values in a vector

170 Views Asked by At

I'm using Matlab to create a GUI. Herefore I'm using the guide function of matlab. I want to store the slider values in a vector. I am doing this in the callback function:

for i = 1:10
X(i) = get(handles.slider1,'Value');
end

But this results in a vector that stores the same value 10 times. What I actually want is to store the last 10 values of the slider in a vector. Any ideas?

1

There are 1 best solutions below

2
On BEST ANSWER

I would suggest to create a 1 x 10 vector of zeros when starting the GUI, i.e. in the OpeningFcn of the GUI:

handles.X = zeros(1,10);
guidata(hObject,handles);        % Update handles variable

Then in the Callback function of the slider, you always shift the vector one to the right and add the new value in the first place:

x =  get(handles.slider1,'Value');
handles.X = [x, handles.X(1:end-1)];
guidata(hObject,handles);         % Update handles variable

Now X always contains the last 10 values of the slider value, X(1) is the last value and so on.

Before the slider hasn't been moved 10 times, some of the values will not be correct, i.e. they will just be zero. If this is a problem, you could grow the X vector dynamically in the Callback.