Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
User interface 使用一行代码在MATLAB中返回弹出菜单选择_User Interface_Matlab_Popupmenu - Fatal编程技术网

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

User interface 使用一行代码在MATLAB中返回弹出菜单选择,user-interface,matlab,popupmenu,User Interface,Matlab,Popupmenu,我有一个GUI,它在另一个回调中使用弹出菜单中的选择。有没有办法只在一行中返回popupmenu的选定值,而不创建任何临时变量?我尝试了几种解决方案,但我只使用一个临时变量管理了两行: 三行: list=get(handles.popupmenu1,'String'); val=get(handles.popupmenu1,'Value'); str=list{val}; 两行: temp=get(handles.popupmenu1,{'String','Value'}); str=temp

我有一个GUI,它在另一个回调中使用弹出菜单中的选择。有没有办法只在一行中返回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}};
有人能把它减少到一个吗

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

这里有一行代码:

str = getCurrentPopupString(handles.popupmenu1);
下面是
getCurrentPopuString

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

我知道这不是您想要的答案,但它确实回答了您提出的问题:)

要使它成为一个单行程序,我只需创建自己的函数(即
getMenuSelection
),如他回答中所示。如果你真的想要一个真正的一行,这里有一个使用:

很难看,很难读。我肯定会写我自己的函数

编辑:这里有一个稍微短一点(但仍然同样难看)的一行,使用:


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

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

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

我不知道你为什么要把它缩短成一行,除非你只是好奇怎么做。三行选项是最清晰、最容易理解的,也是我在编写任何代码时都会用到的选项。@gnovice我主要想知道它是否可以实现。也许是因为MATLAB没有一个功能来处理一个get呼叫而引起的好奇和烦恼的混合。@Jonas:Touché,为我的强迫症问题提供了一个实用的解决方案。你必须从strcmp中删除逻辑not,因为:if~ishandle(hh)| strcmp(get(hh,'Type'),'popupmenu尽管这是一个古老的答案,它仍然非常有用!对于字符串值为非单元格的下拉菜单,我用以下方式修改了最后一行:
str=list(val,:)@Josh:谢谢你的提醒。我已经对我的答案进行了相应的编辑。+1是为了给我一个我真正想要的答案,不管这个答案有多不切实际
str = feval(@(x) x{1}{x{2}},get(handles.popupmenu1,{'String','Value'}));
list=get(handles.popupmenu1,'String'); str=list{get(handles.popupmenu1,'Value')};