Changing the value in the popupmenu

The first item of my popupmenu is an instruction so it really isn't a value, but MATLAB seems to take it as a value whenever i accidentally pick it. Let's say I want to assign "5" to my second item, how do I make it show 5 instead of 2 because whenever I do the calculation MATLAB uses 2 instead of 5.

 Accepted Answer

lookup_table = [nan, 5, 10, -4, 18];
idx = get(hObject, 'value');
if isempty(idx)
... user has not chosen anything
elseif idx > length(lookup_table)
... mismatch between lookup table and number of entries in String property
else
associated_value = lookup_table(idx);
if isnan(associated_value)
... opps, user chose a header.
end
end

3 Comments

This is one of my popupmenu, where exactly should i put the codes?

function firstmenu_Callback(hObject, eventdata, handles)
contents = cellstr(get(hObject,'String'));
popChoice = contents(get(hObject,'Value'));
if strcmp(popChoice,'Black')
    popVal = 0;
elseif strcmp(popChoice,'Brown')
    popVal = 10;
elseif strcmp(popChoice,'Red')
    popVal = 20;
elseif strcmp(popChoice,'Orange')
    popVal = 30;
elseif strcmp(popChoice,'Yellow')
    popVal = 40;
elseif strcmp(popChoice,'Green')
    popVal = 50;
elseif strcmp(popChoice,'Blue')
    popVal = 60;
elseif strcmp(popChoice,'Violet')
    popVal = 70;
elseif strcmp(popChoice,'Grey')
    popVal = 80;
elseif strcmp(popChoice,'White')
    popVal = 90;
end
popChoice = contents{get(hObject,'Value')};
[tf, idx] = ismember(popChoice, {'Black', 'Brown', 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Violet', 'Grey', 'White'});
if ~tf
  ... user choose a header or has not made any choice
else
  popVal = 10 * (idx-1);
end

The code doesn't seem to work either it is still showing the same value

 function firstmenu_Callback(hObject, eventdata, handles)
contents = cellstr(get(hObject,'String'));
popChoice = contents{get(hObject,'Value')};
[tf,idx] = ismember(popChoice, {'Black', 'Brown', 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Violet', 'Grey', 'White'});
if ~tf
    ... user choose a header or has not made any choice
else
popVal = 10*(idx-1);
end

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!