从数据库值填充Matlab GUI列表框 [英] Populating a Matlab GUI Listbox from Database Values

查看:318
本文介绍了从数据库值填充Matlab GUI列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am relatively new to GUI's in Matlab, and I have created a simple GUI using GUIDE. I want to connect to a database (already defined and working!) and populate a listbox with the values from the database so the user can choose which to use (in this case they are chemical compounds). I haven't been able to find a good tutorial or clues on how to populate the listbox in this way. So far, I have:

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个项目的整个列表!

What would be the simplest way to populate a listbox from a database (or large array) like this? As of right now, the listbox is populated with only the first item in names, which is because somehow names contains only the first item. Although, if I just display 'data.name', I get the entire list of 300 items in the list!

推荐答案

I得到它了!所以,问题是,我正在将data.name转换为一个字符 - >最初它是一个单元格。因此,我在for循环中添加了 names(i)= data(i).name; ,并删除了 names = data.name; code>它现在填充所有的化合物的名称!工作功能如下:

I got it! So, the problem was that I was converting the data.name to a character -> originally it was a cell. Thus, I added names(i) = data(i).name; in the for loop, and removed names=data.name; It is now populated with all of the names of the compound! The working function looks like this:

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

这篇关于从数据库值填充Matlab GUI列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆