登录论坛

查看完整版本 : 使用一行代码在MATLAB中返回popupmenu选择


poster
2019-12-10, 20:41
我有一个GUI,它在另一个回调中使用来自popupmenu的选择。有没有一种方法可以在不创建任何临时变量的情况下仅在一行中返回popupmenu的选定值?我尝试了几种解决方案,但仅用一个临时变量管理了两行:

三行:

list=get(handles.popupmenu1,'String'); val=get(handles.popupmenu1,'Value'); str=list{val}; 两行:

temp=get(handles.popupmenu1,{'String','Value'}); str=temp{1}{temp{2}}; 任何人都可以将其剃光到一个吗?

PS,这是一个动态菜单,所以我不能只使用get(handles.popupmenu1,'Value')并完全忽略字符串组件。



回答:

这里是单线:

str = getCurrentPopupString(handles.popupmenu1); 这是getCurrentPopupString的定义

function str = getCurrentPopupString(hh) %# getCurrentPopupString returns the currently selected string in the popupmenu with handle hh %# could test input here if ~ishandle(hh) || strcmp(get(hh,'Type'),'popupmenu') error('getCurrentPopupString needs a handle to a popupmenu as input') end %# get the string - do it the readable way list = get(hh,'String'); val = get(hh,'Value'); if iscell(list) str = list{val}; else str = list(val,:); end 我知道这不是您要找的答案,但确实可以回答您提出的问题:)



更多&回答... (https://stackoverflow.com/questions/2760024)