🎉Community Raffle - Win $25

An exclusive raffle opportunity for active members like you! Complete your profile, answer questions and get your first accepted badge to enter the raffle.
Join and Win

xline Delete Issue

Hi everyone,

I face a problem, when I delete xline in figure. I want to create new xline when push the button, then remove the old xline. I use GUI button for this. When I push the button, it is work for two steps. However, when I push it third time, it is not remove the xline. Then, it works up to two steps. After that I face this problem again. Only one curve should remain in the figure. However, It holds ('Curve 2, Curve 4, Curve 5, Curve 7, Curve 9......') in the figure. If I write delete and create command in OML Command Window, I don't face any problem. I face a problem, when I call this command with function. Is this a bug about Compose ?

 

I am sharing this code in the below.

global second_line;

global sc_Value;

function button_callback(handle,callstate)

      global second_line;

      global sc_Value;

      try

           delete(second_line);

      end

      second_line=xline(get(sc_Value,'value'));

end

Find more posts tagged with

Sort by:
1 - 3 of 31

    Hi Batuhan,

    Can you share the full GUI so I can test a possible solution on my side?

    Rafael

    Hi Rafael Sánchez,

    I am sorry for the late reply. I can't share the full GUI model. However, I am sharing an example about it. You can examine it below.

     

    -----------------------------------------------------------------------------------------------------------

    global first line

     

    Dialog = figure ('units', 'pixels', 'position', [0 0 410 310]...

    ,'numbertitle','off', 'name','Dialog');

    undock (Dialog);

     

    uiPushButton = uicontrol ('parent', Dialog, 'style', 'pushbutton'...

     ,'string','TEST'...

     ,'backgroundimage',''...

     ,'callback', 'uiPushButton_callback', 'interruptible','off'...

     ,'enable', 'on'...

     ,'units', 'normalized', 'position',[0.30 0.20 0.25 0.10]...

     ,'fontname', 'MS Shell Dlg', 'fontangle','regular', 'fontweight', 'normal', 'fontsize',8.25 ...

     ,'backgroundcolor', [246 246 246] ...

     , 'tag','','tooltipstring','','userdata',[],'visible','on');

     

    figure 2

    x = [0 1 2 3 4 5]

    y = [5 2 8 4 1 12]

    plot(x,y)

     

    function uiPushButton_callback(handle,callstate)

       global first_line

       try

           delete(first_line);

       end

       first_line = xline(rand,'color','k');

    end

    Hi Rafael Sánchez,

    I am sorry for the late reply. I can't share the full GUI model. However, I am sharing an example about it. You can examine it below.

     

    -----------------------------------------------------------------------------------------------------------

    global first line

     

    Dialog = figure ('units', 'pixels', 'position', [0 0 410 310]...

    ,'numbertitle','off', 'name','Dialog');

    undock (Dialog);

     

    uiPushButton = uicontrol ('parent', Dialog, 'style', 'pushbutton'...

     ,'string','TEST'...

     ,'backgroundimage',''...

     ,'callback', 'uiPushButton_callback', 'interruptible','off'...

     ,'enable', 'on'...

     ,'units', 'normalized', 'position',[0.30 0.20 0.25 0.10]...

     ,'fontname', 'MS Shell Dlg', 'fontangle','regular', 'fontweight', 'normal', 'fontsize',8.25 ...

     ,'backgroundcolor', [246 246 246] ...

     , 'tag','','tooltipstring','','userdata',[],'visible','on');

     

    figure 2

    x = [0 1 2 3 4 5]

    y = [5 2 8 4 1 12]

    plot(x,y)

     

    function uiPushButton_callback(handle,callstate)

       global first_line

       try

           delete(first_line);

       end

       first_line = xline(rand,'color','k');

    end

    Hi Batuhan. Instead of trying to delete the line, I would toggle its visibility off. You can try something like this:

    close all, clear, clc

    Dialog = figure('units', 'pixels', 'position', [0 0 410 310]...
    ,'numbertitle','off', 'name','Dialog');

    undock(Dialog);

    uiPushButton = uicontrol('parent', Dialog, 'style', 'pushbutton'...
    ,'string','TEST'...
    ,'backgroundimage',''...
    ,'callback', 'uiPushButton_callback', 'interruptible','off'...
    ,'enable', 'on'...
    ,'units', 'normalized', 'position',[0.30 0.20 0.25 0.10]...
    ,'fontname', 'MS Shell Dlg', 'fontangle','regular', 'fontweight', 'normal', 'fontsize',8.25 ...
    ,'backgroundcolor', [246 246 246] ...
    , 'tag','','tooltipstring','','userdata',[],'visible','on');

    % Create new figure and axes
    fig = figure(2);
    ax = axes(fig, 'tag', 'Axes');

    % Mock data for figure
    x = [0 1 2 3 4 5];
    y = [5 2 8 4 1 12];

    % Plot mock data on axes
    h = plot(ax, x,y);

    function uiPushButton_callback(handle,callstate)
        
        % Get axes handle within this function
        ax = findobj('tag', 'Axes');
        % Get handles of lines currently present
        presentLines = get(ax, 'children');
        
        % Disable most recently-added line if there's more than 1
        if numel(presentLines) > 1
            lastLine = get(ax, 'children')(1);
            set(lastLine, 'visible', 'off');
        end
        
        % Create new line
        newLine = xline(ax, rand, 'color', 'k');
        
    end

    I'm attaching the script as well.

    Rafael