www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/@mbcmodel/@data/ImportFromFile.m

    function D = ImportFromFile(D, filename, filetype,varargin)
%IMPORTFROMFILE Import data from a file.
%
%   IMPORTFROMFILE(DATA, FILENAME)
%   IMPORTFROMFILE(DATA, FILENAME, FILETYPE)
%   IMPORTFROMFILE(DATA, FILENAME, FILETYPE,varargin)
%     Extra arguments are passed to the file read function. For example, it
%     is possible to specify a sheet name for an Excel file. 
% 
%      ImportFromFile(DATA,FILENAME,'Excel file',SHEETNAME);
%  
%   See also mbcmodel.data.ImportFromMBCDataStructure, mbcmodel/CreateData,
%   mbcmodel.project.CreateData, mbcmodel.data.Append

%   Copyright 2004-2010 The MathWorks, Inc.

error(mbcnargchk(2, Inf, nargin, 'mbc:mbcmodel:data'));

% Make sure that the data object supports this function
D.pCheckIsBeingEdited;

try
    if nargin < 3
        filetype = 'auto';
    end    
    if ~exist(filename,'file')
        % try looking on the path
        fullname = which(filename);
        if isempty(fullname)
            error(message('mbc:mbcmodel:dataloading:InvalidFileType', filename))
        end
    else
        [pathstr,fname,ext] = fileparts(filename);
        if isempty(pathstr)
            fullname = fullfile(pwd,[fname ext]);
        else
            fullname = filename;
        end
    end
    % Get the internal object
    ssf = D.Object;
    % Get the import function applicable to the filename
    importFcn = mbcgetdataloadingfcnfor(fullname, filetype);
    % Call the import function - this could call user code which might
    % throw an error so catch it
    try
        [OK, msg, out] = feval(importFcn, fullname, sweepset2struct(sweepset),false,varargin{:});
    catch E
        if strcmp(E.identifier,'MATLAB:TooManyInputs')
            try
                % old API without GuiMode
                [OK, msg, out] = feval(importFcn, fullname, sweepset2struct(sweepset));
            catch E
                error(message('mbc:mbcmodel:data:LoadError', filename, E.message));
            end
        else
            error(message('mbc:mbcmodel:data:LoadError', filename, E.message));
        end
    end
    % Did the file successfully load?
    if ~OK
        error(message('mbc:mbcmodel:data:LoadError', filename, msg));
    end
    % Convert the output to a sweepset
    ss = struct2sweepset(sweepset, out);
    % And set the filename of where the data came from 
    ss = set(ss, 'FileName', filename);
    % Set the sweepset for the sweepsetfilter (This will trigger an UpdateAll in the
    % sweepsetfilter and hence all relavant events will be triggered)
    ssf = setSweepset(ssf, ss);
    % Do we need to set any default test groupings?
    if isempty(get(ssf, 'definetests'))
        ssf = applyDefaultTestDefinition(ssf);
    end

    % set name of data object to filename
    [p,f,e]= fileparts(fullname);
    set(ssf,'Label',f);
    % Finally update the internal reference
    D.Object = ssf;
catch E
    [mnemonic, component] = mbcGetLastError(E);
    switch [component ':' mnemonic]
        case 'dataloading:InvalidFileType'
            error(message('mbc:mbcmodel:data:InvalidFileType', E.message));
        case 'data:LoadError'
            error(message('mbc:mbcmodel:data:LoadError3', E.message));
        otherwise
            error(message('mbc:mbcmodel:data:UnknownError7'));
    end    
end