error using list box, please help!!!

my gui has a listbox1.
the list box has 2 (values).
for each values i have two different numbers, and i want to change the string and the handle of a edit text (edit1).
when i run the gui and click for the first time on the listbox every thing is OK.....but when i click for the second time appears the following error :
??? Error using ==> set
Invalid handle object.
Error in ==> GUI>listbox1_Callback at 110
set(handles.edit1,'String',X)
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> GUI at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)GUI('listbox1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
________________________
here is my code:::
function listbox1_Callback(hObject, eventdata, handles)
Z=get(hObject,'value')
if Z==1
X=120;
elseif Z==2
X=240;
end
set(handles.edit1,'String',X)
handles.edit1=X
guidata(hObject,handles)
___________
thank for helping my solving this issue.

 Accepted Answer

you are overwriting handles.edit1, see:
handles.edit1 = X
handle with the number X. Remove this and it should be ok.

3 Comments

i remove that line from the code, the listbox1 just change the String of edit1 and works fine. but if i modify the edit1 (tyoe any number), and then click over listbox1, the same error apears.
----
this is the code for the edit1 that im using:
-----
function edit1_Callback(hObject, eventdata, handles)
N=str2double(get(hObject,'String'))
handles.edit1=N
guidata(hObject,handles)
----
You're still re-assigning the handle to something. DON'T DO THAT! Use set() instead.
To be clear, DON'T DO THIS:
handles.edit1 = N;
Do this instead:
set(handles.edit1, 'String', N); % N is a string.
If N is a number, call num2str or sprintf() to make it a string.
strN = sprintf('N = %d', N);
set(handles.edit1, 'String', strN); % strN is a string.
You have the same basic problem as Robert pointed out earlier: you are writing over the *handle* with a numeric value.
What is that you want to have happen when you press return in the edit box? You get the number, and then what do you want to do with it? Write the number back to the same edit box in some particular format or even just write it back so that the representation gets "cleaned up" (e.g., blanks removed, any exponential format changed to floating point)? Or when you finish the edit, do you want to find the listbox entry that is numerically closer and make that the highlighted listbox entry? Or you want to add it as (always) the 3rd listbox entry? Or you want to add it as a listbox entry provided that it is not already there? Or you want to update the code for the listbox callback so that the next time the current listbox entry is selected, it will be the entry now pulled out of the edit box that would later get put in to the edit box instead of the 120 / 240 ? Or ... ??

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!