MATLAB: Store data from slider to share it with a pushbutton in MATLAB GUI

guipushbuttonsliderstore data

Hi,
I am trying to create a GUI where the user can select a value using a slider and then can obtain a plot by pressing a pushbutton. This is the code for the slider:
% --- Executes on slider movement.
function TP_Callback(hObject, eventdata, handles)
% hObject handle to TP (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
handles.TP=get(hObject,'Value'); Tp=round(handles.TP); set(handles.text11,'String',Tp);
Then I have a pushbutton callback with the code. Here I need to use the value selected with the slider but it seems that it is not stored as I get the following warning:
"Undefined function or variable 'Tp'."
Could I get some help here?
thank you in advance.
Ana

Best Answer

In your pushbutton callback, do this:
sliderValue = handles.Tp.Value;
If you want to call it Tp, you can do that instead:
Tp = handles.Tp.Value;
This uses modern OOP syntax from R2014b or later. If you have an antique version of MATLAB, use get():
Tp = get(handles.Tp, 'Value');