Buttondownfcn gui轴

时间:2014-08-04 21:48:14

标签: matlab matplotlib

我有一个Gui,它有一个轴......在轴上我可以用绘图绘制线条..但我想选择一条轴线...我试过用buttondownfcn ..但它没有'工作..我有一个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});

帮帮我..我可以选择并删除一行吗???或移动???有一些方法可以做到这一点???? 这对我来说非常重要..请!!帮帮我!:)

1 个答案:

答案 0 :(得分:0)

以下示例代码可帮助您根据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结构(a.k.a。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