2013-06-05 29 views
0

我想在Matlab GUI的彈出式菜單中獲取所選項目的索引。爲此,我寫了這個在彈出的回調函數:在Matlab彈出菜單中返回所選項目的索引

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 

可以肯定theItemcontents。我如何獲得索引?我在哪裏做錯了?

回答

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, :)獲取所選項目。

希望有幫助!

+0

是的,後來我找到了解決方案。無論如何,非常感謝。 – newzad

相關問題