PDA

查看完整版本 : 在MATLAB GUI函数的末尾更新列表框并将变量更改为旧值


poster
2019-12-14, 20:38
下面的GUI只是在列表框中列出了一些数据,对其进行了绘制,并为用户提供了从列表框中删除条目的选项。在用户删除条目后(试图运行该条目时,它应该很明显),我被撞到墙上,试图使列表框正确更新。

我遇到的两个问题是:


虽然按钮的回调确实确实从数组x正确删除了该条目,但回调完成后,数组x会还原回原始x数组(因此,值会以某种方式恢复自身)。

我尝试使用“ drawnow”刷新列表框以反映当前的x数组(已被修改,至少在下面发出drawow的位置)。虽然没有任何变化。如何使用新的x数组值刷新列表框?

---------------- GUI代码---------

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