Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3676648
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:07:29+00:00 2026-05-19T03:07:29+00:00

The below GUI simply lists some data in a listbox, plots it, and gives

  • 0

The below GUI simply lists some data in a listbox, plots it, and gives the user the option to delete entries from the listbox. I’m hitting my head against the wall trying to get to listbox updated correctly after the user deletes an entry (it should be obvious when you run it).

The 2 problems I have are:

  1. While the callback to the pushbutton does indeed delete the entry correctly from array x, once the callback finishes, the array x reverts back to the original x array (thus, the value somehow restores itself).

  2. I try to use “drawnow” to refresh the listbox to reflect the current x array (which has been modified, at least where drawnow is issued below). Nothing changes though. How can I refresh the listbox with the new x array values?

—————-GUI code———

function testgui
clear; close all;
% Create the data
x = (10:20)'; % example data

% Create a figure
f = figure('Name', 'GUI Test',...
  'NumberTitle', 'off',...
  'MenuBar', 'none',...
  'Units', 'pixels',...
  'Position', [100 100 700 800],...
  'Visible', 'off');

% Create a set of axes
a = axes('Parent', f,...
  'Units', 'pixels',...
  'Position', [230 50 450 700]);

% Create a pushbutton for modifying x array
pb_x = uicontrol('Style', 'pushbutton', ...
  'Parent', f,...
  'String', 'Delete Selection',...
  'Units', 'pixels',...
  'Position', [50 7 100 35],...
  'Callback', @pb_change_x);

plot(x,'bo'); % graph
xlabel('Index Number'); ylabel('Variable x');

% Create a listbox to hold the x values
y=strtrim(cellstr(num2str(x)));
lb = uicontrol('Style', 'listbox',...
  'Parent', f,...
  'String', y,...
  'Units', 'pixels',...
  'Position', [50 50 100 700],...
  'Callback', @list_change);

set(f,'Visible','on')


function list_change(varargin)
    % Runs when the selection in listbox is changed
    curIdx = get(lb,'Value');
    % Index the value
    curVal = str2double(cell2mat(y(curIdx)));

    % Get position of curVal in x
    posVal = find(ismember(x,curVal));

    % Clear all the children currently on the axes
    delete(get(a,'Children'))

    % Plot the one data point of interest
    plot(posVal,curVal,'bo','MarkerFaceColor','b','MarkerSize',10)
    xlabel('Index Number'); ylabel('Variable x');
    % Plot all the data
    hold on
    plot(x,'bo')
    hold off
end

function pb_change_x(varargin)
    % Runs when DUT <<< pushbutton is clicked
    % Obtain selected index
    curIdx = get(lb,'Value');
    % Index the value
    curVal = str2double(cell2mat(y(curIdx)));

    % Get position of curVal in x
    posVal = ismember(x,curVal);
x(posVal) = []; % HELP (why doesn't value stick when this function returns?)

    % Redraw figure (want listbox to update)
drawnow; % HELP (how to refresh listbox with current x array?)

    % Clear all the children currently on the axes
    delete(get(a,'Children'))

    % Plot all the data
    plot(x,'bo'); xlabel('Index Number'); ylabel('Variable x');
end

end 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:07:29+00:00Added an answer on May 19, 2026 at 3:07 am

    You have correctly identified the fix. More generally, I would say that you need to separately update each of the items that encode your GUI’s state (i.e. each of the items stored independently by the GUI).

    Specifically, your implementation separately stores to the GUI both ‘x’, your array of data, in the plot and ‘y’, the cell array that has strings for the listbox names for each item in ‘x’. When you created your listbox, ‘lb’, you provided ‘y’ as the ‘String’ attribute. At that point the listbox is separately storing its own copy of ‘y’ independent of your ‘y’ and ‘x’ variables.

    When you update your plot, you are resetting the GUI’s value of ‘x’, but as you discovered you also need to update the listbox’s ‘String’ attribute (its copy of ‘y’). In this case, the way you chose seems most straightforward, but a more general approach is to repeat the steps used to generate ‘y’ from ‘x’ (after updating ‘x’) and then use “set(lb, ‘String’, y);” to reset the GUI’s version of ‘y’.

    Hope that is helpful! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below are lines from the c++ programming language template<class T > T sqrt(T );
Below is my table, a User could have multiple profiles in certain languages, non-English
Below I have a very simple example of what I'm trying to do. I
Below are two ways of reading in the commandline parameters. The first is the
Below is my current char* to hex string function. I wrote it as an
Below is part of the XML which I am processing with PHP's XSLTProcessor :
Below is my $.ajax call, how do I put a selects (multiple) selected values
Below is the code of a simple html with a table layout. In FF
Below is my (simplified) schema (in MySQL ver. 5.0.51b) and my strategy for updating
Below is a stored procedure to check if there is a duplicate entry in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.