error using list box, please help!!!

3 views (last 30 days)
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

Robert Cumming
Robert Cumming on 12 Jan 2012
you are overwriting handles.edit1, see:
handles.edit1 = X
handle with the number X. Remove this and it should be ok.
  3 Comments
Image Analyst
Image Analyst on 12 Jan 2012
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.
Walter Roberson
Walter Roberson on 12 Jan 2012
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)

Community Treasure Hunt

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

Start Hunting!