从数据库值填充Matlab GUI列表框

时间:2012-07-15 16:54:08

标签: database matlab user-interface listbox matlab-guide

我对Matlab中的GUI相对较新,我使用GUIDE创建了一个简单的GUI。我想连接到一个数据库(已定义并正在工作!)并使用数据库中的值填充列表框,以便用户可以选择使用哪个(在这种情况下,它们是化合物)。我无法找到一个好的教程或线索如何以这种方式填充列表框。到目前为止,我有:

function load_listbox(hObject,handles) 

    conn = database('antoine_db','','');
    setdbprefs('datareturnformat','structure'); %sets the db preferences to a structure
    query = 'SELECT ID,"Compound Name" FROM antoine_data ORDER BY ID';

    result = fetch(conn,query);

    %%The following creates a structure containing the names and ID's
    %%of everything in the database
    data = struct([]);
    for i=1:length(result.ID)
        data(i).id =   result.ID(i);
        data(i).name = char(result.CompoundName(i));
    end


    names = data.name;
    handles.compounds = names;
    whos;
    set(handles.listbox1,'String',handles.compounds,'Value',1);

    handles.output = hObject;

    % Update handles structure
    guidata(hObject, handles);

end

从这样的数据库(或大型数组)填充列表框的最简单方法是什么?到目前为止,列表框中只填充了名称中的第一个项目,这是因为某些名称只包含第一个项目。虽然,如果我只显示'data.name',我会在列表中获得300个项目的完整列表!

1 个答案:

答案 0 :(得分:1)

我明白了!所以,问题在于我将data.name转换为字符 - >原来它是一个细胞。因此,我在for循环中添加了names(i) = data(i).name;,并删除了names=data.name;现在它已经填充了所有化合物的名称!工作函数如下所示:

function load_listbox(hObject,handles) 

    conn = database('antoine_db','','');
    setdbprefs('datareturnformat','structure'); %sets the db preferences to a structure
    query = 'SELECT ID,"Compound Name" FROM antoine_data ORDER BY ID';

    result = fetch(conn,query);

    %%The following creates a structure containing the names and ID's
    %%of everything in the database
    data = struct([]);
    for i=1:length(result.ID)
        data(i).id =   result.ID(i);
        data(i).name = (result.CompoundName(i));        %this is a cell
        names(i) = data(i).name;
    end



    handles.compounds = names;
    set(handles.listbox1,'String',handles.compounds,'Value',1);

handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

end