Matlab uicontrol 列表框在关闭时返回而不是立即返回

Posted

技术标签:

【中文标题】Matlab uicontrol 列表框在关闭时返回而不是立即返回【英文标题】:Matlab uicontrol listbox return on close rather than immediately 【发布时间】:2015-10-30 19:46:08 【问题描述】:

我使用 matlab 中的 lbox2 示例创建了一个列表框,它一运行就会返回值。但是我希望用户单击一个值,并让列表框返回该值。还应该注意的是,我使用的是过时版本的 matlab,2007b。

例如,

    [handle,value] = myListbox;
    uiwait(handle);

但是 value 立即返回并且是空白的,因为用户还没有进行更改以选择任何内容。由于代码一直等到对话框关闭,这是期望的,但是当窗口关闭时,它的数据会丢失......我认为。

知道用户触发回调后如何获取返回值吗?

完整代码:

function varargout = ListBox(varargin)
% LBOX2 Application M-file for lbox2.fig
%   LBOX2, by itself, creates a new LBOX2 or raises the existing
%   singleton*.
%
%   H = LBOX2 returns the handle to a new LBOX2 or the handle to
%   the existing singleton*.
%
%   LBOX2('CALLBACK',hObject,eventData,handles,...) calls the local
%   function named CALLBACK in LBOX2.M with the given input arguments.
%
%   LBOX2('Property','Value',...) creates a new LBOX2 or raises the
%   existing singleton*.  Starting from the left, property value pairs are
%   applied to the GUI before lbox2_OpeningFunction gets called.  An
%   unrecognized property name or invalid value makes property application
%   stop.  All inputs are passed to lbox2_OpeningFcn via varargin.
%
%   *See GUI Options - GUI allows only one instance to run (singleton).
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2000-2006 The MathWorks, Inc.

% Edit the above text to modify the response to help lbox2

% Last Modified by GUIDE v2.5 30-Oct-2015 13:36:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',          mfilename, ...
               'gui_Singleton',     gui_Singleton, ...
               'gui_OpeningFcn',    @lbox2_OpeningFcn, ...
               'gui_OutputFcn',     @lbox2_OutputFcn, ...
               'gui_LayoutFcn',     [], ...
               'gui_Callback',      []);
if nargin && ischar(varargin1)
   gui_State.gui_Callback = str2func(varargin1);
end

if nargout
    [varargout1:nargout] = gui_mainfcn(gui_State, varargin:);
else
    gui_mainfcn(gui_State, varargin:);
end
% End initialization code - DO NOT EDIT


% --- Executes just before lbox2 is made visible.
function lbox2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to lbox2 (see VARARGIN)

% Choose default command line output for lbox2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

if nargin == 3,
    initial_dir = pwd;
elseif nargin > 4
    if strcmpi(varargin1,'dir')
        if exist(varargin2,'dir')
            initial_dir = varargin2;
        else
            errordlg('Input argument must be a valid directory','Input Argument Error!')
            return
        end
    else
        errordlg('Unrecognized input argument','Input Argument Error!');
        return;
    end
end
% Populate the listbox
load_listbox(initial_dir,handles)
% Return figure handle as first output argument

% UIWAIT makes lbox2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = lbox2_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout1 = handles.output;
varargout2 = handles.systemType;

% ------------------------------------------------------------
% Callback for list box - open .fig with guide, otherwise use open
% ------------------------------------------------------------
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox1 contents as cell array
%        contentsget(hObject,'Value') returns selected item from listbox1

get(handles.systemType);
get(handles.listbox1,'Value');
get(handles.figure1,'SelectionType');


if strcmp(get(handles.figure1,'SelectionType'),'open')
    index_selected = get(handles.listbox1,'Value');
    type_list = get(handles.listbox1,'String');
    setappdata(hObject, 'systemType', type_listindex_selected); 

    close(handles.figure1);
end
% ------------------------------------------------------------
% Read the current directory and sort the names
% ------------------------------------------------------------
function load_listbox(dir_path,handles)

%dir_struct = dir(dir_path);
%[sorted_names,sorted_index] = sortrows(dir_struct.name');
handles.file_names = 'option1', 'option 2', 'option 3','option 4';
handles.systemType = '';
%handles.sorted_index = sorted_index;
guidata(handles.figure1,handles)
set(handles.listbox1,'String',handles.file_names,...
    'Value',1)


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Add the current directory to the path, as the pwd might change thru' the
% gui. Remove the directory from the path when gui is closed 
% (See figure1_DeleteFcn)
setappdata(hObject, 'StartPath', pwd);
addpath(pwd);


% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Remove the directory added to the path in the figure1_CreateFcn.
if isappdata(hObject, 'StartPath')
    rmpath(getappdata(hObject, 'StartPath'));
end

所以双击item后,函数应该返回'option1' 'option 2' 'option 3' or 'option 4' 谢谢。

【问题讨论】:

我不太确定您所说的 lbox2 示例 是什么意思 - 您能否提供更完整的代码? 你指的是this example吗?如果是这样,您是否修改了原始代码(例如原始代码,只返回一个输出)。你能发布你的整个代码吗? 是的,那个例子。此外,添加了列表框的完整代码。 @il_raffa 【参考方案1】:

我没有让回调关闭框,而是将回调中的 close(h) 更改为 uiresume(h),然后使用以下代码使用列表框并从中获取值。

%prompt listbox
h = ListBox();
uiwait(h);

%get response from list box
appdata = getappdata(h);
type = appdata.systemType; %this was the information i was trying to get

%close the window
close(h);

【讨论】:

以上是关于Matlab uicontrol 列表框在关闭时返回而不是立即返回的主要内容,如果未能解决你的问题,请参考以下文章

用户更新后从 Matlab 编辑 uicontrol 中删除光标?

Matlab Imppoint 和 Uicontrol

如何将图像放在 Matlab uicontrol 按钮上?

UIcontrol 按钮以在 matlab 中绘制工作区中的数据

MATLAB uicontrol 回调返回矩阵

如何从matlab中用户定义的函数返回一个值,该函数的回调在uicontrol中指定