How can I move the vertical line in an axes by a slider within a GUI?

19 visualizzazioni (ultimi 30 giorni)
Hi, Guys. I want to change the vertical line position by a slider. The codes as follows.
function slider_wf_Callback(hObject, eventdata, handles)
% hObject handle to slider_wf (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
global FLAG_DATA_LOADED;
if FLAG_DATA_LOADED == 1
slider_value = int32(get(hObject,'Value'));
set(handles.text_cur_frame_num, 'String',num2str(slider_value));
axes(handles.axes_waveform);
h = vline(slider_value/20, 'r-');
end
guidata(hObject, handles);
However, when I moved the slider, the previous lines still existed. How to solve this problem?Thanks in advance.

Risposta accettata

Adam Danz
Adam Danz il 13 Dic 2019
Modificato: Adam Danz il 16 Dic 2019
Instead of drawing a new line using vline(), draw the line just once and then use the line handle to update its positoin within the slider callback function.
This demo uses xline() instead of the 3rd party vline() function (which isn't a matlab function).
xline() requires >=r2018b
The demo should give you an idea of the code's structure - you can adapt it to your needs.
function slider_wf_Callback(hObject, eventdata, handles)
persistent lineHandle
if isempty(lineHandle) || ~isvalid(lineHandle)
% Create line: 1st input is axis handle
lineHandle = xline(handles.Axes, 0,'r-');
end
% Update line position
slider_value = int32(get(hObject,'Value'));
lineHandle.Value = slider_value/20;
end
*Not tested

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by