0

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

4

1 回答 1

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

截屏

于 2013-05-08T10:02:28.833 回答