two listboxes in a GUI

조회 수: 2 (최근 30일)
Jules Ray
Jules Ray 2013년 8월 26일
dear all
i'm trying to create a gui taht consits in 2 listboxes. The idea is load some data in the listbox 2 based in the selection on the lisbox 1. The data consist in 1:1:10 numbers. (10=as)
For this i create a script in the listbox 1 callback, that loads a file contained in a directory, the script read this file and put the contents on the listbox 2. However i have a problem setting the habdles of listbox 2.
here is the script in the callback of listbox 1 that must display the list in listbox 2.
%LISTBOX 1:
function listbox 1_Callback(hObject, eventdata, handles)
handles.output = hObject;
%store the selected station name from listbox as station (input for
%terracem scripts)
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; %selection of item in listbox 1 stored
%put profile names of profiles in next listbox/ new version
station=handles.statsel;
dirstations=handles.stationsdir;
%maindir=handles.maindir;
cd(dirstations)
cd(station)
sel=(handles.statsel);
load(sprintf('%s_swaths.mat',sel)); %load a file in the folder
as=nim
nameprof=(1:1:as);
%%%%%%%%%for instance, in this case the values of nim or as=10%%%%%%%
%%%%%here the program gives an error%%%%%%%%%%%%%%%%
set(handles.listbox2,'value', nameprof)
%save data
guidata(hObject, handles);
all the script seems to work good until i set the handles for the next listbox (listbox2), where the loaded data (nameprof =1:1:10) must be displayed.
here is the error:
Reference to non-existent field
'listbox2'.
Error in
Terracem_gui_v5b>stationslist_Callback
(line 793)
set(handles.listbox2,'value', nameprof)
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Terracem_gui_v5b (line 42)
gui_mainfcn(gui_State,
varargin{:});
Error in
@(hObject,eventdata)Terracem_gui_v5b('stationslist_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
i dont know why i cannot set the callback for the listbox 2, someone have an idea to solve this....... thanks and thanx in advance

채택된 답변

David Sanchez
David Sanchez 2013년 8월 28일
To set the list of numbers on the your list2, this has to be done by setting the 'String' property of list2. The list of elements to be inserted in list2 has to be a cell array.
nameprof=1:10;
set( handles.list2,'String',num2cell(nameprof) );

추가 답변 (1개)

Vishal Rane
Vishal Rane 2013년 8월 28일
Check if the tag of the second listbox is 'listbox2'. Also confirm if the second listbox has been created when you are trying to access it.
  댓글 수: 2
Jules Ray
Jules Ray 2013년 8월 28일
Thanks.. i realize that i didnt save the .fig file, only the .m ... :p
Jules Ray
Jules Ray 2013년 8월 28일
i have been trying again but the problem continues. THe listbox2 is created and the tag is correct, and all seems to be in order but this line dosent work:
set(handles.listbox2,'String',num2cell(af));
is ok to set the handles of the listbox2 into the callback of listbox1? i'm trying to load data in the listbox2 by selecting data in listbox1, is possible to do that? i mean, by clicking an item in listbox1 a set of string (1:10) will be loaded into the listbox2.
what i'm doing bad?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by