www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/mbcgetdataloadingfcnfor.m

    function fcn = mbcgetdataloadingfcnfor(filename, filetype)
%MBCGETDATALOADINGFCNFOR Find an MBC data loading function for a file
%
% FCN = MBCGETDATALOADINGFCNFOR(FILENAME, FILETYPE)
%
% Find a suitable file loader for a data file held in the FILENAME
% variable. FILETYPE holds the file type of the data contained in FILENAME
% and is the FILETYPE specified for the registered file loader. If FILTTPYE
% is 'auto' or not specified then the file loading function is determined
% by the extension on FILENAME.
% 
% See also MBCGETDATALOADINGFCN, MBCCHECKINDATALOADINGFCN

% Copyright 2004-2011 The MathWorks, Inc.

if nargin < 2
    filetype = 'auto';
end
% Get the currently installed DataLoading Functions
fcns = mbcgetdataloadingfcn();
% Find valid data import functions
validFunctions = [];
% Need to check which functions still exist
for i = 1:length(fcns)
    func = fcns(i).function;
    if strcmp(class(func), 'function_handle')
        % Convert function handles to chars
        func = func2str(func);
    end
    % Does this function exist as an m, p or mex file?
    funcExist = exist(func);
    if funcExist == 2 || funcExist == 3 || funcExist == 6
        % If so add it to the list of valid functions
        validFunctions = [validFunctions i];
    end
end
importFcns = fcns(validFunctions);
% Get the extension from the filename
[~, ~, ext] = fileparts(filename);
% Auto or manual file detection
if strcmpi(filetype, 'auto')
    % Automatic detection of file type based on the extension of the file
    % being loaded
    filterSpecs = vertcat(importFcns.filterSpec);
    filters = filterSpecs(:,1);
    found = [];
    for i = 1:length(filters)
        ind = strfind(lower( filters{ i } ),lower( ext ));
        % Do any of these found indices completely match ext. Is this the last extension in the
        % list or is it immediately followed by a ';'
        nextCharInd = ind + length(ext);
        for j = 1:length(ind)
            if nextCharInd(j) <= length(filters{i}) && filters{i}(nextCharInd(j)) ~= ';'
                % If it is not the last ext or followed by ';' then remove from the found list
                ind(j) = [];
            end
        end
        if ~isempty(ind) 
            found = [found i];
        end
    end
    % Can we decide which sort of file we are opening?
    if isempty(found) 
        error(message('mbc:dataloading:InvalidFileType', ext));
    elseif length(found) > 1
        error(message('mbc:dataloading:InvalidFileType1'));
    end
    fileTypeValue = found; 
else
    % Manual file type detection
    fileTypeValue = find( strncmpi( filetype,{ importFcns.fileType },length(filetype) ) );
    % Check one was found
    if isempty(fileTypeValue)
        error(message('mbc:dataloading:InvalidFileType2', filetype));        
    end
    % Make sure only one is used
    fileTypeValue = fileTypeValue(1);
end
% Return the 
fcn = importFcns(fileTypeValue).function;