www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/mv_getmatrix.m
function [m,ok,str]=mv_getmatrix(varargin) %MV_GETMATRIX % % X=MV_GETMATRIX displays a gui for choosing a matrix from the base workspace % [X,OK]=MV_GETMATRIX also returns a flag indicating whether a matrix was % was selected or the cancel button pressed % X=MV_GETMATRIX(SIZE) where SIZE is a vector of numbers indicating that only % matrices with the given size in each dimension should be displayed. Use % a NaN to indicate that any size is allowed in that dimension. % X=MV_GETMATRIX(SIZE,TYPE) also indicates the type of variables that should be % shown. TYPE is either a string or a cell array of strings. % [X,OK,STR]=MV_GETMATRIX(SIZE,TYPE) also returns the name of the variable in % the base workspace. % Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc. if nargin==0 action='create'; elseif isnumeric(varargin{1}) action='create'; else action=varargin{1}; end switch lower(action) case 'create' figh = xregdialog('resize', 'off', ... 'name', 'Choose Matrix'); xregcenterfigure(figh, [160 250]); ud.list=uicontrol('Style','listbox',... 'Parent',figh,... 'Position',[10 40 140 200],... 'Callback',[mfilename '(''dblclick'');'],... 'BackgroundColor',[1 1 1]); % ok and cancel okbtn = uicontrol('Style','pushbutton',... 'Parent',figh,... 'Position',[13 7 65 25],... 'String','OK',... 'Callback',[mfilename '(''ok'');']); uicontrol('Style','pushbutton',... 'Parent',figh,... 'Position',[82 7 65 25],... 'String','Cancel',... 'Callback',[mfilename '(''cancel'');']); if nargin==0 dosizecheck=false; dotypecheck=false; elseif nargin==1 dosizecheck=true; dotypecheck=false; elseif nargin>1 if isempty(varargin{1}) dosizecheck=false; dotypecheck=true; else dosizecheck=true; dotypecheck=true; end varargin{2}=lower(varargin{2}); end % get string for listbox S=evalin('base','whos'); if dosizecheck || dotypecheck ncompdims=length(varargin{1}); % make NaN exclusion vector NaNs=isnan(varargin{1}); % loop over variables inds=[]; for n=1:length(S) sok=1; if dosizecheck vardims=S(n).size; % buffer vardims to same length as dim filter if length(vardims)>ncompdims vardims=vardims(1:ncompdims); end if length(vardims)<ncompdims vardims(end+1:ncompdims)=1; end ok=((vardims==varargin{1}) | NaNs); if ~all(ok); sok=0; end end tok=1; if dotypecheck if ~any(strcmpi(S(n).class,varargin{2})) tok=0; end end if tok && sok % save index inds=[inds n]; end end else inds=':'; end if isempty(inds) str='No matrices found'; else str={S(inds).name}; end set(ud.list,'String',str); set(figh,'UserData',ud); figh.showDialog(okbtn); ud=get(figh,'UserData'); if isempty(ud) %cancel situation m=[]; str=''; if nargout>1 ok=0; end else m=ud; str=m.matnm; m=m.mat; if nargout>1 ok=1; end end delete(figh) case 'dblclick' figh=gcbf; if strcmp(get(figh,'SelectionType'),'open') mv_getmatrix('ok'); end case 'cancel' figh=gcbf; set(figh,'UserData',[]); set(figh,'Tag','finished', 'Visible', 'off'); case 'ok' figh=gcbf; ud=get(figh,'UserData'); val=get(ud.list,'Value'); str=get(ud.list,'String'); if ischar(str) str={str}; end if strcmp(str(1),'No matrices found') % this is really a cancel condition set(figh,'UserData',[]); else matnm=str{val}; mat=evalin('base',matnm); ud.mat=mat; ud.matnm=matnm; set(figh,'UserData',ud); end set(figh,'Tag','finished', 'Visible', 'off'); end