首页 > 解决方案 > 如何通过 MATLAB GUI 中的滑块移动轴中的垂直线?

问题描述

我想通过滑块更改垂直线位置。代码如下。

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

但是,当我移动滑块时,前面的行仍然存在。如何解决这个问题呢?

图像样本 提前致谢。

标签: matlablinemove

解决方案


我没有 function vline,但我假设它将句柄返回给 a Line。您必须要么删除旧线并绘制新线,要么操纵现有线的位置。在这两种情况下,您都必须将手柄存放在某处。在 GUIDE 中,该handles结构用于此目的。

这是删除旧的解决方案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

通过操纵现有的第二次尝试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

推荐阅读