Problem with listbox in Matlab GUI.

6 次查看(过去 30 天)
Warning: single-selection listbox control requires that Value be an integer within String range Control will not be rendered until all of its parameter values are valid
I have 2 listbox. First listbox is show X coordinates and second one show the correspondind Y values for each X. eg. When I press X = 100 on the second listbox I see e.g Y = 100,101,102 ect. Sometimes when I press some values on Y listbox, and after that I change value on X listbox the Y listbox disappear and the error above shows.
Picture shows how it should work
Picture shows how it work when I press value higher than 100 on Y then when I press 442 X the Y listbox disappear.
I didn'y show the code because it's a lot of code and I think it's something wrong with listbox by itself. This is working properly. When I press 442 X it's working well but the error happens under specific conditions.
Any idea what might go wrong ?

采纳的回答

Image Analyst
Image Analyst 2013-9-28
Nothing is wrong with the listbox itself. There is something wrong in the callback for the X listbox, which you didn't want to show us. So you're either going to have to figure it out yourself of show us your code. In your callback for the X listbox, you're either
(A) doing something like loading the listbox for Y without the same number of entries it used to have like Y used to have 20 items in it and the selection was 18 and then you load it up with a cell array of 17 strings.
set(handles.listboxY, 'String', cellArrayOf17Strings);
Since the old (and current) value of 18 is more than the number of items now in the listbox (17), it won't be shown.
Or
(B) You're setting a value for the listbox for Y that is greater than the number of strings in it. For example Y has 20 items and then in your X callback you do
set(handles.listboxY, 'Value', 21);
Since 21 is more than the number of items in the Y listbox (20), it won't be drawn.
Bottom line, look for where you reference handles.listboxY in your callback for listboxX.
  1 个评论
Masoud Daryoushi
Masoud Daryoushi 2018-4-3
I have same problem, but you didn't propose a solution.
When I choose some elements and click on the push button ">" it works properly such as below:
But when I choose for example '60' and push button ">" the left list box disappears:
Here is the code in push button ">"
if true
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
currentSizes=get(handles.listbox2,'string');
selectedSizes = get(handles.listbox1, 'string');
selectedItems = get(handles.listbox1, 'value');
selectedSize = selectedSizes(selectedItems);
newList=[currentSizes;selectedSize];
N = cellfun( @str2double, regexp(newList, '\d+', 'match') );
[~,I] = sort(N);
newList = newList(I);
set(handles.listbox2,'string',newList);
newList2=selectedSizes;
newList2(selectedItems)=[];
N2 = cellfun( @str2double, regexp(newList2, '\d+', 'match') );
[~,I2] = sort(N2);
newList2 = newList2(I2);
set(handles.listbox1,'string',newList2);
end

请先登录,再进行评论。

更多回答(3 个)

Jan
Jan 2013-9-27
The warning message is clear: The values of the 'Min' and 'Max' properties define the listbox as a single selection control. Then exactly one element must be selected. If you want to select no or multiple elements, the distance between 'Min' and 'Max' must be greater than 1. Exmaple:
h = uicontrol('Style', 'listbox', 'String', {'1', '2'}); % Ok
pause(1);
set(h, 'Value', []); % Nothing selected, warning appears
pause(1)
set(h, 'Max', 2); % Listbox appears again
  2 个评论
Jonasz
Jonasz 2013-9-28
But I select only one value in both listboxes they are two different one.
Jan
Jan 2013-9-28
@Jonasz: Ok, but because you did not show us the corresponding code at first, this guess would at least result in the same error message. The more details you post to describe a problem, the less time is wasted with wrong guesses.

请先登录,再进行评论。


Jonasz
Jonasz 2013-9-28
This is my X callback. The function xy finds which Y are connected to which X .
function X_listbox_Callback(hObject, eventdata, handles)
% hObject handle to X_listbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns X_listbox contents as cell array
% contents{get(hObject,'Value')} returns selected item from X_listbox
id=get(hObject,'Value');
list=get(hObject,'String');
item_selected=str2double(list{id});
dire=get(handles.text_dir,'String');
yid=xy(item_selected,dire);
yid=yid';
set(handles.Y_listbox,'String',cellstr(num2str(yid)));

Jonasz
Jonasz 2013-9-28
Ok I figure it out. The problem was to reset the Value of Y listbox to eg. one that all the values of X have.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by