0

I would like to programme a dynamic dropdown menu where a user can load an dataset and it will show the dataset when the user click on it. I could manage to get a dynamic string list but I don't know how to make the switch-case dynamic.

val=get(hObject,'value');
axes(handles.axes1);
switch val
    case 1
        imshow('trollface.jpg');
    case 2
        imshow('Forever-alone-face.png');
    case 3
        imshow('yao-ming.png');
    otherwise
end
guidata(hObject,handles);

Here is an example. However, the number of cases should be dynamic in a way that if a user Click A then the data A should be loaded. Do you have any suggestions how I can do this? Thanks

Kamu
  • 67
  • 1
  • 1
  • 8
  • 1
    Would a lookup e.g. map conatianer help? http://stackoverflow.com/questions/9850007/how-to-use-hash-tables-dictionaries-in-matlab - then you can just find the value associated with a key, and populate these dynamically – doctorlove Jan 12 '17 at 16:19
  • What is `hObject`? Just a dropdown? If so, what's in that list? A list of filenames? Why not just do `strings = get(hObject, 'String'); imshow(strings{get(jObject, 'Value')})` – Suever Jan 12 '17 at 16:21
  • yes, just a dropdown menu. The case is, that the dropdown menu is dynamic. That means, the dropdown list will extend whenever a user load a new data set.In the list I stored the filenames. – Kamu Jan 13 '17 at 14:13

1 Answers1

0

You can use val to index into a cell array instead of using a switch-case:

images = {
    'trollface.jpg'
    'Forever-alone-face.png'
    'yao-ming.png'};

imshow( images{ val})
rafa
  • 326
  • 3
  • 9
  • The case is, that the dropdown menu is dynamic. That means, the dropdown list will extend whenever a user load a new data set.In the list I stored the filenames. – Kamu Jan 13 '17 at 14:13
  • Yes, that was meant as an example of the indexing; Instead of the hard coded list I provided, use images = get( hObject,'String') – rafa Jan 31 '17 at 14:44