Listbox content from structure hierarchy (GUIDE)

2 ビュー (過去 30 日間)
Manuel
Manuel 2013 年 7 月 30 日
I have a listbox in which one I would like to set as content the subfield names of a structure.
Example: 'Database' is the structure which have inside the fieldnames Car, Bus and Motorbike. These fieldnames are 1x1 struct (e.g Database.Car) and are used as subfields.
Then I want to create a listbox which contains the subfields and mantain this listbox updated since different subfields will be added.
Thanks in advance,

採用された回答

Jan
Jan 2013 年 7 月 31 日
Here "Car" etc are the "fields", not the "subfields" (which would mean something like: DataBase.Vehicle.Car).
DataBase.Car = 1;
DataBase.Bus = 2;
DataBase.Motorbike = 3;
List = fieldnames(DataBase);
uicontrol('Style', 'listbox', 'String', List);
Keeping the listbox "updated" when the struct is changed, is very complicated. You need an object oriented approach to start a callback, when the data are changed. But you can fake it by not adding further fields manually, but when you use a dedicated function for this:
function S = myAddField(S, Field, Data)
S.(Field) = Data;
updateGui(S); % <-- Implementation depends on your GUI
  3 件のコメント
Manuel
Manuel 2013 年 8 月 6 日
@Jan, Hello Jan, I have still some problems with the listbox probably I misunderstund some concepts. As you told me before to show the fieldnames in the listbox content we need the next code:
List = fieldnames(DataBase);
uicontrol('Style', 'listbox', 'String', List);
but the 'uicontrol' command above should be located on the listbox_CreateFcn function? :
function listbox_CreateFcn(hObject, eventdata, handles)
or it is an independent way to set the listbox content? Because I tried in the listbox_CreateFcn and does not work.
Thank you for your time,
Regards
Manuel
Manuel 2013 年 8 月 7 日
Problem solve, can be done inside the 'CreateFcn(hObject, eventdata, handles)' function as:
List = fieldnames(Database);
set(hObject,'String',List);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by