Matlab - 更新列表框以显示新数据


这样做

的目的只是将用户为他们制作的绘图提供的名称存储到列表框中,以便他们可以看到他们所做操作的历史记录,例如1)用户绘制一条线并将其称为"第一行",因此列表框显示"第一行"2)然后用户绘制一个矩形并将其称为"矩形",因此列表框显示"第一行" "矩形"

到目前为止,我拥有的相关代码如下所示。

 name = inputdlg('Enter a name for the Object', 'Line Name');
        %get the current list box contents and store to a handle array
        handles.currentHistory = get(handles.historyList, 'String');
        %now add the name the user enters to this array
        handles.currentHistory(size(handles.currentHistory)+1) = name;
        %now update the history list
        set(handles.historyList, 'String', handles.currentHistory);

对于第一个,它的输出很好,但随后继续如下,假设 3 个对象被调用第一、第二和第三个。

第一

第二第二

第三第二第三

正在努力理解为什么它以这种方式输出,并想知道我如何简单地让它更新。

干杯。

你可以这样做:

handles.currentHistory{end+1} = name; % add element after the last one

为此,您必须正确初始化handles.currentHistory

handles.currentHistory = {};

最新更新