MATLAB: Generating a clickable list of strings and have a new variable selected with that string

guiselect string

I am looking to populate a list of statistics, essentially a list of strings, representing a set of statistics names (i.e. volume, intensity, Surface area). I want to be able to visualize all the stats and select a one of them, and set it as a new variable.
I would like to have some kind of simple GUI interface, I was trying to make a dropdown menu of some kind using the following, but I am not sure how to get the string out. I want to be able to have the script pause until i chose one.
vSpotsStatsNameUnique = {'Volume';'MeanIntensity';'SurfaceArea};
S.fh = figure('units','pixels',...
'position',[300 300 300 110],...
'menubar','none',...
'name','Spot Statistics',...
'numbertitle','off',...
'resize','off');
S.pp = uicontrol('style','pop',...
'units','pixels',...
'position',[20 10 260 40],...
'string', vSpotsStatsNameUnique);
S.ed = uicontrol('style','edit',...
'units','pix',...
'position',[20 60 260 30],...
'fontsize',16,'string','none');
Any help would be greatly appreciated. Thankyou.

Best Answer

Hello,
if I understood correctly, you want to get the actual selected string from popup uicontrol.
A simple way to do that is presented below:
vSpotsStatsNameUnique = {'Volume'; 'MeanIntensity'; 'SurfaceArea'};
handles.figure = figure('units', 'pixels', ...
'position', [300 300 300 110], ...
'menubar', 'none', ...
'name', 'Spot Statistics', ...
'numbertitle', 'off', ...
'resize', 'off');
handles.popup = uicontrol('style','pop', ...
'units', 'pixels', ...
'position', [20 10 260 40], ...
'string', vSpotsStatsNameUnique, ...
'Callback', @myFunc);
handles.edit = uicontrol('style', 'edit', ...
'units', 'pixels', ...
'position', [20 60 260 30], ...
'fontsize', 16, 'string', 'none');
guidata(handles.figure, handles);
Note that a callback was added to the popup, pointing to the function @myFunc. Then, to access the selected string from popup control you can use the following method.
function myFunc(hObj,~,~)
while ~strcmp('figure', get(hObj, 'type')) && ~isempty(hObj) % While hObj isn't a figure
hObj = get(hObj, 'Parent');
end
hObj = guidata(hObj); % Get the handles structure.
PopStrings = get(hObj.popup, 'String'); % Read the string list on the popup control.
PopIdx = get(hObj.popup, 'Value'); % Read the selected index.
PopSelected = PopStrings{PopIdx}; % Your string value goes in here.
disp(PopSelected);
end
Hope this helps you.