function [hList,nList]=bfitgetdata(figureH)
%BFITGETDATA Finds all lines of a figure 
% and returns a list of those handles with appropriate names  

%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 2000/09/01 13:27:48 $

axesList = findobj(figureH, 'type', 'axes');
taglines = get(axesList,'tag');
notlegendind = ~(strcmp('legend',taglines));
h = findobj(axesList(notlegendind),'type','line');
h = flipud(h(:)); % so comes out as added to figure
bfitdataall = getappdata(figureH,'_Basic_Fit_Data_All');

% remove from h anything that has zdata, i.e. not 2 dimensional
for i = 1:length(h)
	if ~isempty(get(h(i), 'zdata'))
	   	h(i) = [];
	end
end

% reorder h so fits, residuals, eval results, data stats, if any, are at the end
bfitind = logical(zeros(size(h)));
for i = 1:length(h)
    value = getappdata(h(i),'_bfit');
    if isempty(value)
        fitappdata.type = 'data potential';
        fitappdata.index = [];
        setappdata(h(i),'_bfit',fitappdata);
        if ~isempty(bfitdataall) & ~any(h(i) == bfitdataall)
            bfitdataall(end + 1) = h(i);
        else
            bfitdataall(1) = h(i);
        end
    elseif ~isequal(value.type,'data') & ~isequal(value.type,'data potential')
        bfitind(i) = logical(1); 
    end
end % for
setappdata(figureH,'_Basic_Fit_Data_All', bfitdataall);

% This is all data or potential data in the figure that isn't created by the gui (fit or data stat)
% and the axes that goes with them.
bfitdata = h(~bfitind);
dataaxescell = get(bfitdata,'parent');
if iscell(dataaxescell)
    dataaxes = unique([dataaxescell{:}]);
else % scalar
    dataaxes = dataaxescell;
end
setappdata(figureH,'_Basic_Fit_Axes_All',dataaxes);

% code to get selected handles
selectedHandles = [];
aFigObjH = getobj(figureH);
if ~isempty(aFigObjH)
    dragBinH = aFigObjH.DragObjects;
    if ~isempty(dragBinH.Items)
        objectVector = struct(dragBinH.Items);
        if ~isempty(objectVector)
            selectedHandles = [objectVector.HGHandle];
        end
    end
end
selected = ismember(h,selectedHandles);

% Move the selected lines to the front, then the unselected lines,
% then the selected fits followed by the unselected fits.
% If you want data and fit/stat lines, use this:
% h = [h(selected & ~bfitind); h(~selected & ~bfitind); 
%    h(selected & bfitind); h(~selected & bfitind)];
% If you want just data lines, use this:
h = [h(selected & ~bfitind); h(~selected & ~bfitind)]; 

% If data is not tagged, give it a name.
% Put the tag or name in the appdata
[hList, nList] = datanames(h,figureH);

%-----------------------------------------------------------------
function [hList,tagList]=datanames(h,figureH)

%Gets name descriptions for handles
%returns two column cell arrays
if length(h)==0
    hList={};
    tagList={};
    countstart = 0;
else
    
    hList=num2cell(h);
    hList=hList(:);
    
    countstart = getappdata(figureH,'Basic_Fit_Data_Counter');
    tagList=get(h,'tag');
    if length(h)==1
        tagList={tagList};
    end
    
    % if appdataname exists, use it.
    % else use 'tag'
    % else create a name using the counter.
    for i=1:length(h)
        appdataname = getappdata(h(i),'_bfit_dataname');
        if isempty(appdataname)
            if isempty(tagList{i})
                tagList{i} = ['data ',int2str(countstart)];
                countstart = countstart + 1;
            end
            d = tagList{i};
            % name must be a char row vector.
            if ~isequal(size(d,1),1)
                d = d';
                d = (d(:))';
            end
            setappdata(h(i),'_bfit_dataname',d);
        else
            tagList{i} = appdataname;
        end
    end
end
setappdata(figureH,'Basic_Fit_Data_Counter',countstart);

