1

私は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アイテムのリスト全体が表示されます。

4

1 に答える 1

1

わかった!したがって、問題は、data.nameを文字に変換していたことでした->元々はセルでした。names(i) = data(i).name;したがって、 forループに追加し、削除しました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
于 2012-07-15T21:39:50.290 に答える