0

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

4

1 回答 1

3

Value弹出菜单的 设置为所选项目的索引。因此,如果您选择了三个项目中的第二个,则Value将为 2,您可以使用selectedIdx = get(hObject, 'Value').

String弹出菜单的 可以设置为字符串胞数组,每个项目一个;或字符数组,每个项目一行。

如果您已将 设置String为字符串元胞数组,则可以使用items = get(hObject, 'String'); selectedItem = items{selectedIdx}.

如果您已将 设置String为字符数组,则可以使用items = get(hObject, 'String'); selectedItem = items(selectedIdx, :).

希望有帮助!

于 2013-06-06T14:03:14.653 回答