使用一行代码返回 MATLAB 中的弹出菜单选择

发布于 2024-08-31 15:28:54 字数 448 浏览 2 评论 0原文

我有一个 GUI,它在另一个回调中使用弹出菜单中的选择。有没有一种方法可以仅在一行中返回弹出菜单的选定值而不创建任何临时变量?我已经尝试了几种解决方案,但我只用一个临时变量管理了两行:

三行:

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') 并完全忽略字符串组件。

I have a GUI which uses a selection from a popupmenu in another callback. Is there a way to return the selected value of the popupmenu in only one line without creating any temporary variables? I've tried several solutions, but I've only managed two lines with one temporary variable:

Three lines:

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

Two lines:

temp=get(handles.popupmenu1,{'String','Value'});
str=temp{1}{temp{2}};

Can anyone shave it down to one?

PS, It's a dynamic menu, so I can't just use get(handles.popupmenu1,'Value') and ignore the string component altogether.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来日方长 2024-09-07 15:28:54

这是一句俏话:

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

我知道这不是您正在寻找的答案,但它确实回答了您提出的问题:)

Here's a one-liner:

str = getCurrentPopupString(handles.popupmenu1);

And here's the definition of 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

I know that's not the answer you were looking for, but it does answer the question you asked :)

ぽ尐不点ル 2024-09-07 15:28:54

我知道这很愚蠢,但我无法抗拒:

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

我知道这不是你的意思,但就像上面的其他答案一样,它确实回答了你的问题......:-)

I know this is stupid, but I couldn't resist:

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

I know that's not what you meant, but like the other answers above, it does answer your question... :-)

时光清浅 2024-09-07 15:28:54

为了使其成为一行,我只需创建自己的函数(即 getMenuSelection),例如 Jonas 在他的回答中进行了说明。如果您真的想要真正的一句台词,这里有一个使用CELLFUN

str = cellfun(@(a,b) a{b},{get(handles.popupmenu1,'String')},{get(handles.popupmenu1,'Value')});

非常丑陋且难以阅读。我肯定会编写自己的函数。

编辑:这是一个稍短(但仍然同样难看)的单行代码,使用 FEVAL

str = feval(@(x) x{1}{x{2}},get(handles.popupmenu1,{'String','Value'}));

To make it a one-liner, I would simply create my own function (i.e. getMenuSelection) like Jonas illustrates in his answer. If you really want a true one-liner, here's one using CELLFUN:

str = cellfun(@(a,b) a{b},{get(handles.popupmenu1,'String')},{get(handles.popupmenu1,'Value')});

Very ugly and hard to read. I'd definitely go with writing my own function.

EDIT: And here's a slightly shorter (yet still equally ugly) one-liner using FEVAL:

str = feval(@(x) x{1}{x{2}},get(handles.popupmenu1,{'String','Value'}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文