How to automatically fill a listbox with for loop ?

3 ビュー (過去 30 日間)
Joel Sande
Joel Sande 2015 年 7 月 30 日
コメント済み: Joel Sande 2015 年 7 月 31 日
%% I have this example of code
for i = 1:1:10
T = randi([0 10],1,1); % random number between 0 and 10
R = num2str(T);
listbox1.add(i,R); %%this line is adding only one number to the list
%%I want to add all these numbers in the JList
end
  3 件のコメント
Joel Sande
Joel Sande 2015 年 7 月 30 日
The first step is working now. I have a textfield in the Figure. I want to write the selected number in that textfield.
See attached File to understand.
Joel Sande
Joel Sande 2015 年 7 月 30 日
In this last version, the selected item index is reported on the textfield. I want the item string, not its index.

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

採用された回答

Image Analyst
Image Analyst 2015 年 7 月 31 日
Try this:
numItems = 10;
for k = 1 : numItems
T = randi([0 10],1,1); % random number between 0 and 10
oneCell = {sprintf('%d', T)};
listboxItems(k) = oneCell;
end
% Optionally, print cell array to command window so we can see it.
celldisp(listboxItems)
% Send this cell array to the listbox
set(handles.listbox1, 'String', listboxItems);
  1 件のコメント
Joel Sande
Joel Sande 2015 年 7 月 31 日
Thanks Image Analyst, It is working very well now.
When I select it and press a button, the selected number is sent to the textfield.
Thank a lot.
Now I can complete the code with real data.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by