我想在Matlab GUI的弹出菜单中获取所选项目的索引。为此我在弹出回调函数下写了这个:
contents = cellstr(get(h0bject,'String'));
theItem = contents{get(h0bject,'Value')};
theindex = find(contents == theItem);
Matlab回归:
Undefined function 'eq' for input arguments of type 'cell'
然后我写了
contents = cellstr(get(h0bject,'String'));
theItem = contents{get(h0bject,'Value')};
contents = cell2mat(contents):
theItem = str2num(theItem);
theindex = find(contents == theItem);
Matlab返回
index = Empty matrix: 0-by-1
确保theItem
位于contents
。我怎样才能获得索引?我在哪里做错了?
答案 0 :(得分:3)
弹出菜单的Value
设置为所选项目的索引。因此,如果您选择了三个项目中的第二项,Value
将为2,您可以使用selectedIdx = get(hObject, 'Value')
获取它。
pop-pup菜单的String
可以设置为 字符串的单元格数组,每个项目一个;或字符数组,每个项目一行。
如果您将String
设置为字符串的单元格数组,则可以使用items = get(hObject, 'String'); selectedItem = items{selectedIdx}
获取所选项目。
如果您已将String
设置为字符数组,则可以使用items = get(hObject, 'String'); selectedItem = items(selectedIdx, :)
获取所选项目。
希望有所帮助!