I would like to get the index of selected item in a pop-up menu in the Matlab GUI. For that I wrote this under pop-up callback function:
contents = cellstr(get(h0bject,'String'));
theItem = contents{get(h0bject,'Value')};
theindex = find(contents == theItem);
Matlab returns:
Undefined function 'eq' for input arguments of type 'cell'
Then I wrote
contents = cellstr(get(h0bject,'String'));
theItem = contents{get(h0bject,'Value')};
contents = cell2mat(contents):
theItem = str2num(theItem);
theindex = find(contents == theItem);
Matlab returns
index = Empty matrix: 0-by-1
For sure theItem
is in the contents
. How can I get the index? Where am I doing wrong?
The Value
of a pop-up menu is set to the index of the selected item. So if you've selected the second of three items, the Value
will be 2, and you can fetch it using selectedIdx = get(hObject, 'Value')
.
The String
of a pop-pup menu can be set to either a cell array of strings, one per item; or a character array, with one row per item.
If you've set the String
to be a cell array of strings, you can get the selected item using items = get(hObject, 'String'); selectedItem = items{selectedIdx}
.
If you've set the String
to be a character array, you can get the selected item using items = get(hObject, 'String'); selectedItem = items(selectedIdx, :)
.
Hope that helps!