Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 使用一行代码在MATLAB中返回popupmenu选择

我有一个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 我知道这不是您要找的答案,但确实可以回答您提出的问题:)



更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 02:54


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.