LISTBOX, PLEASE HELP ME WITH THE LISTBOX

Hello, i have posted some questions before about listbox but none give an answer....
my question is simple
i have this matrix:
a=1:1:10 % this is 1 2 3 4 5 6 7 8 9 10
i would like to load these numbers into a listbox, but i dont know how, must i change these numbers to char???
how ca i do this???? thanks in advance

1 commentaire

evgeny shumsky
evgeny shumsky le 10 Sep 2019
Modifié(e) : evgeny shumsky le 10 Sep 2019
if you wont it to be one under another
Mat=[1:10];
Mat = mat2cell(num2str(Mat')); % mat=mat' converts from row vector to column vector
set(handles.listbox,'string',Mat); % listbox ->tag of the listbox
1-10.JPG

Connectez-vous pour commenter.

 Réponse acceptée

David Sanchez
David Sanchez le 27 Août 2013
The array has to be transformed into a cell arrary:
a=1:1:10;
b=num2cell(a);
Then, send it to the popup menu:
set(handles.my_pop,'String',b);

7 commentaires

Thanxs David and Wayne, now i got the input format fot the listbox, but i still have a probem with the listbox. So i'm using two listboxes:
i put the transformation to cell array
(a=1:1:10; b=num2cell(a);)
in the callback of the "Listbox_1"because the number (10) comes from the selction in the Listbox_1. And works ok, because the number (10) is selected successfully in the Listbox_1, creating also the cell array successfully.
The problems now starts when i use this line:
set(handles.Listbox_2,'string',b)
in the lower part of the Listbox_1, this give me a error that says:
Reference to non-existent field
'Listbox_2'.
I created the Listbox_2 already, and have a callback also for this listbox, but i don't know why i cannot set the handles of Listbox_2.
Do you have any idea???? should i put some code into the callback of Listbox_2?
thanks for your answer
What do you mean by:
The problems now starts when i use this line:
set(handles.Listbox_2,'string',b)
in the lower part of the Listbox_1?
You can't have one listbox in the lower part of another listbox. Do you just want to add some more items onto the lower part of Listbox_1, or do you want to load up an entirely different listbox, Listbox_2? Are you sure the tag is Listbox_2 and not listbox2, listbox_2, or Listbox2? MATLAB is case sensitive, and of course the underlines have to be correct.
Jules Ray
Jules Ray le 27 Août 2013
Hello Image analyst.... i mean that i set the handles of the Listbox_2 in the callback of the Listbox_1. In this case Listbox_1 and Listbox_2 are two different listboxes... separated but the data selected in Listbox_1 (by double clicking) determines the data that will be load in Listbox_2... that's what i mean...
I dont know why but when i set the handles of Listbox_2 into the callback of Listbox_1 say the error that i posted before... my question is: why this happens; may handles of listboxes needs to be created in the callback of each listbox before to be set?...
Image Analyst
Image Analyst le 27 Août 2013
No. It has the handle. You just didn't type it correctly. Set a breakpoint in the listbox_1 callback and type handles on the command line to list all members of the handles structure in the command window. Then copy it and paste it back here so we can see what handles actually contains (i.e. the field names).
here the handles:
andles =
figure1: 174.0012
Untitled_1: 202.0012
uipanel3: 199.0012
uipanel2: 183.0012
uipanel1: 175.0012
text2: 201.0012
stationslist: 200.0012
text13: 198.0012
t2: 197.0012
ti: 196.0012
t1: 195.0012
text11: 194.0012
text10: 193.0012
text9: 192.0012
difu: 191.0012
slope: 190.0012
text5: 189.0012
text3: 188.0012
text4: 187.0012
difusionbuton: 186.0012
swatbuton: 185.0012
sureornot: 1
cleanall: 182.0012
fullpath2: 181.0012
fullpath1: 180.0012
pushbutton5: 179.0012
text1: 178.0012
stationsdir: [1x86 char]
maindir: [1x77 char]
output: 200.0012
statsel: 'CLMO'
as you see, the gui that i'm creating is big but two things are interesting: 1) the listbox_1 keep the old name that i give it (stationslist), i dont know why. and the second 2) the listbox_2 didint appears in the handles list. Both are placed in different panels.
i dont know why this two issues happens...
regards
just in case: here is the code on listbox_1 that should put the strings in the listbox_2, but is not working:
function listbox_1_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
handles.output = hObject;
contents = cellstr(get(hObject,'String')); %returns stationslist contents as cell array
station_selection=char(contents{get(hObject,'Value')}) %returns selected item from stationslist
handles.statsel=station_selection;
%put profile names of profiles in LISTBOX_2
station=handles.statsel;
dirstations=handles.stationsdir;
cd(dirstations)
cd(station)
sel=(handles.statsel);
load(sprintf('%s_swaths.mat',sel));
as=nim
af=(1:1:as)
a=num2cell(af)
%a=10
set(handles.listbox_2,'String',a)
guidata(hObject, handles);
cheers
Image Analyst
Image Analyst le 28 Août 2013
What are all those things? Are any of them listboxes? Are you saying that your tag in GUIDE says listbox_1 and listbox_2 but in the code it doesn't? How are you running it? Are you clicking the green triangle in GUIDE or in MATLAB? I suspect that the figure you think you're running, you really aren't - you're running a different one.

Connectez-vous pour commenter.

Plus de réponses (1)

Wayne King
Wayne King le 27 Août 2013
a = inputdlg('Enter your vector');
% when dialog comes up enter, 1:10, click OK
a = str2num(cell2mat(a));
a is now a double precision vector

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by