在Matlab弹出菜单中返回所选项目的索引

时间:2013-06-05 23:13:55

标签: matlab user-interface popup indices

我想在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。我怎样才能获得索引?我在哪里做错了?

1 个答案:

答案 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, :)获取所选项目。

希望有所帮助!