How to automatically fill a listbox with for loop ?

%% 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

%% with this last version, it is working, but the numbers don<t
%%go to the next line
for i = 1:1:10
T = randi([0 10],1,1); % random number between 0 and 10
R = num2str(T);
handles.list(i)= R;
end
set(handles.listbox1,'String',handles.list)
%%the new numbers don<t go to the next line.
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.
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

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개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2015년 7월 30일

댓글:

2015년 7월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by