2015-10-30 33 views
0

我創建了一個列表框,使用matlab中的lbox2示例,只要它運行它返回值。不過,我希望用戶點擊一個值,並讓列表框返回該值。還應該指出,我使用的是過時的matlab版本2007b。Matlab的uicontrol列表框返回關閉,而不是立即

例如,

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

但是值立即返回,是空的,因爲用戶一直沒有改變,選擇任何事情。由於代碼會一直等到對話框關閉,這是所需的,但是當窗口關閉時,它的數據會丟失......我想。

任何想法如何獲得用戶觸發回調後的返回值?

全碼:

所以經過
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(varargin{1}) 
    gui_State.gui_Callback = str2func(varargin{1}); 
end 

if nargout 
    [varargout{1: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(varargin{1},'dir') 
     if exist(varargin{2},'dir') 
      initial_dir = varargin{2}; 
     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 
varargout{1} = handles.output; 
varargout{2} = 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 
%  contents{get(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_list{index_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 

該項目是雙點擊,函數應該返回「選項1」「選項2」「選項3」或「選項4」 感謝。

+0

我不完全確定你的意思* lbox2例子* - 你能提供更完整的代碼嗎? – zelanix

+0

您是否參考[此示例](http://uk.mathworks.com/help/matlab/creating_guis/interactive-list-box-in-a-guide-gui.html#f6-17499)?如果是這樣,你是否修改了原始代碼(例如原始代碼,只返回一個輸出)。你能發佈你的整個代碼嗎? –

+0

是的,那個例子。還爲列表框添加了完整的代碼。 @il_raffa – Natalie

回答

0

而不是讓回調關閉框,我改變關閉(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);