【问题标题】:Buttondownfcn gui axesButtondownfcn gui 轴
【发布时间】:2014-08-04 21:48:14
【问题描述】:

我有一个 Gui,它有一个轴...在轴上我可以用 plot 画线..但我想选择轴的线..我尝试使用 buttondownfcn..但它没有t 工作.. 我有一个按钮 DELETE,它的回调是:

hold all;
set(handles.axes6, 'HitTest', 'off');
set(handles.axes6,'ButtonDownFcn',('h = copyobj(gcbo,figure)'));
delete_object_axes = findobj(h, 'Type', 'line');

我的代码是:

% --- Executes on mouse press over axes background.
function axes6_ButtonDownFcn(~, ~, handles)
% hObject    handle to axes6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hold all;
set(handles.axes6, 'HitTest', 'off');
set(handles.axes6, 'ButtonDownFcn', {@Delete_Callback, handles}');


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(~, ~, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hold all;enter code here
global h;
set(handles.axes6, 'HitTest', 'off');
h = findobj(handles.axes6, 'Type', 'line');
set(h, 'ButtonDownFcn', {@Delete_Callback});

帮帮我..如何选择和删除一行???或移动???有什么办法吗??? 这对我来说很重要..拜托!!帮帮我!:)

【问题讨论】:

  • 我是否理解正确 - 您希望能够在单击时从轴中选择一条线,并在单击“删除”按钮时将其删除?

标签: matlab matplotlib


【解决方案1】:

以下是基于this discussion in MATLABcentral 的示例代码,可以帮助您实现所需的目标。如果你点击了一行,它的作用是 checl,如果你点击了,它会修改它,如果你再次点击它,它会被删除。您可以将其粘贴到文件中并运行。

function init()
    close all; clear variables; clc;
    ax1=ezplot('sin(x)');
    set(ax1,'ButtonDownFcn',@mouseClick);
end

function mouseClick(source, event)
    if strcmp(get(source,'Type'),'line')
        if get(source,'linewidth')==2
            delete(source);
            return
        end
        %// Store the handle "source" someplace

        %// Demonstration modification:
        set(source,'linewidth',2);
    end
end

编辑:这里有一个更复杂的例子,展示了如何将变量保存到handles 结构(又名guidata):

function init()
    close all; clear variables; clc;
    hLine(1) = ezplot('sin(x)'); hold all; hLine(2) = ezplot('cos(x)');
    set(hLine(:),'ButtonDownFcn',@mouseClick);
    grid on
end

function mouseClick(hObject, eventdata)

 handles = guidata(gcf); %#1

 try
     if handles.delete_object_axes==hObject;
         delete(hObject);
         handles = rmfield(handles,'delete_object_axes');
     else
         set(handles.delete_object_axes,'linewidth',1);
         set(hObject,'linewidth',2);
         handles.delete_object_axes = hObject;
     end     
 catch
     if strcmp(get(hObject,'Type'),'line')
        handles.delete_object_axes = hObject;
        set(hObject,'linewidth',2);
        guidata(gca, handles);
     else
        set(handles.delete_object_axes,'linewidth',1);
     end
     return;
 end

 guidata(gca, handles);

end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 2011-09-29
    • 2016-07-30
    • 1970-01-01
    • 2014-09-03
    • 2012-12-06
    相关资源
    最近更新 更多