清除列表框中的变量

时间:2013-05-08 09:29:26

标签: matlab user-interface

我创建了一个列表框来访问工作区中的变量。我想要 通过突出显示变量来清除工作区中的某些变量 在列表框中。谁能让我知道怎么做?

1 个答案:

答案 0 :(得分:1)

以下是一个示例GUI:

function clear_vars_gui
    %# create user interface
    hFig = figure('Name','Clear Variables', 'NumberTitle','off', ...
        'Menubar','none', 'Position',[300 300 200 300]);
    hList = uicontrol('style','listbox', 'Min',1, 'Max',10, ...
        'Units','normalized', 'Position',[0 0.1 1 0.9], 'Parent',hFig);
    uicontrol('style','push', 'String','clear', 'Callback',@button_cb, ...
        'Units','normalized', 'Position',[0 0 1 0.1], 'Parent',hFig);

    %# initialize listbox
    populateList();

    function populateList()
        %# populate listbox with variable names
        vars = evalin('base','who');
        set(hList, 'String',vars, 'Value',1)
        drawnow
    end
    function button_cb(o,e)
        %# get list of variables and the currently selected items
        vars = get(hList, 'String');
        idx = get(hList, 'Value');
        if isempty(vars), return; end

        %# filter to selected vars
        vars = vars(idx);

        %# clear variables in base workspace
        evalin('base', ['clearvars ' sprintf('%s ',vars{:})]);

        %# update listbox
        populateList();
    end
end

screenshot