meng dai
meng dai

Reputation: 33

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

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?

Image sample Thanks in advance.

Upvotes: 3

Views: 309

Answers (1)

Patrick Happel
Patrick Happel

Reputation: 1351

I don't have a function vline, but I assume that it returns the handle to a Line. You have to either delete the old line and draw a new one or manipulate the position of the already existing line. In both cases, you have to store the handle somewhere. In GUIDE, the handles struct is used for that.

This is the solution deleting the old Line:

if isfield(handles, 'myvline') % on the first run, no handle is available
    delete(handles.myvline);
end
handles.myvline = vline(slider_value/20, 'r-');

% ...

guidata(hObject, handles); % important, used to update the handles struct

Second attempt by manipulating the existing Line

if isfield(handles, 'myvline') % on the first run, no handle is available
    handles.myvline.XData(:) = slider_value/20;
else
    handles.myvline = vline(slider_value/20, 'r-');
end

% ...

guidata(hObject, handles); % important, used to update the handles struct

Upvotes: 1

Related Questions