如何在 MATLAB GUI 中通过滑块移动轴中的垂直线?
How can I move the vertical line in an axes by a slider within a 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);
但是,当我移动滑块时,之前的行仍然存在。如何解决这个问题呢?
提前致谢。
我没有函数 vline
,但我假设它 returns 是 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
我想通过滑块改变垂直线的位置。代码如下
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);
但是,当我移动滑块时,之前的行仍然存在。如何解决这个问题呢?
我没有函数 vline
,但我假设它 returns 是 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