Matlab:在选择项目之前从弹出菜单中获取值

Matlab: Get value from popup menu before choosing an item

我正在开发带有几个弹出菜单的 GUI。问题是,如果您 运行 该程序并且没有从菜单中选择任何内容,则该值设置为上次我 运行 该程序时的值。我在打开函数中得到了这个(tag_port 和 tag_speedunit 是两个弹出菜单的标签):

 set(handles.tag_port,'Value',1);
set(handles.tag_speedunit,'Value',1);

但这行不通。通过更改值,它只会更改 运行 程序时弹出菜单中显示的项目,但如果你不选择任何东西,它仍然停留在上次 运行 时选择的项目上.

我的弹出功能看起来像这样:

function tag_port_Callback(hObject, eventdata, handles)
% That'll give me all the Choices in the popup menu
contents = get(hObject,'String');
% That'll give the value of the specific choice
popChoice = contents{get(hObject,'Value')};
setappdata(0,'popChoice',popChoice);

%end of every funktion
guidata(hObject,handles);

在我的主要函数中,我要求值:

popChoice = getappdata(0,'popChoice')

但是正如我所说,popChoice 不会给我弹出值“1”,除非我通过单击它来选择它。

有什么想法吗???非常感谢。

问题是因为以编程方式设置值不会触发回调,只有用户的明确操作才会触发回调。你真的有两个选择:

  1. 在设置Value的时候在开头设置popChoice值。

    set(handles.tag_port,'Value',1);
    set(handles.tag_speedunit,'Value',1);
    
    % Set the default value here
    setappdata(0, 'popChoice', 'defaultvalue')
    
  2. 在开启函数手动触发回调

    set(handles.tag_port, 'Value', 1)
    set(handles.tag_speedunit, 'Value', 1)
    
    % Trigger the callback
    tag_port_Callback(handles.tag_port, [], guidata(handles.tag_port))