www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/xregImportDataFile.m

    function [ss,OK] = xregImportDataFile(title)
%xregImportDataFile import data file with dialog
%    [data,OK] = xregImportDataFile(title)

%  Copyright 2013-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.


if nargin==0
    title = 'Select data file to import';
end
ss = sweepset;
OK = false;
data = sweepset2struct(ss);

% Get the data using the data loading wizard
p = getpref(mbcprefs('mbc'), 'DataLoading');
% Find valid data import functions
validFunctions = [];
for i = 1:length(p.DataImportFunctions)
    func = p.DataImportFunctions(i).function;
    if isa(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,'file');
    if funcExist == 2 || funcExist == 3 || funcExist == 6
        % If so add it to the list of valid functions
        validFunctions = [validFunctions i]; %#ok<AGROW>
    end
end
importFcns = p.DataImportFunctions(validFunctions);
% Get the returned index from the file open dialog
[fileNames, pathname, fileTypeValue] = uigetfile(vertcat({'*.*' 'All Files (*.*)'}, importFcns.filterSpec), ...
    title, mbcGetPath('mbcmodel', 'Data'),...
    'MultiSelect','on');
if isnumeric(fileNames)
    return
end

if ~iscell(fileNames)
    fileNames = {fileNames};
    
end

fullFileNames = fullfile(pathname, fileNames);
% handle multiple Excel files
[~,~,ext]=cellfun(@fileparts,fullFileNames,'UniformOutput',false);
isExcel = fileTypeValue==2 || (fileTypeValue==1 && all(strcmpi('.xls',ext) | strcmpi('.xlsx',ext)));
if isExcel
     %start Excel - it is more efficient to start Excel once
     excelCOM = xregExcel('start');
     Args = {'',excelCOM};
     closeExcel = onCleanup(@()xregExcel('close', excelCOM));
else
    Args = {};
end
% load first file 
[OK,ss] = loadSingleFile(fullFileNames{1},fileTypeValue,importFcns,data,Args{:});
if OK && length(fullFileNames)>1
    % wait dialog with a stop button
    wb = xregGui.waitdlg('ShowStopButton',true);
    wb.Title = 'Loading data files';
    wb.Waitbar.Max = length(fullFileNames);
    wb.Waitbar.Min = 1;
    wb.Waitbar.Value = 1;
    for i=2:length(fullFileNames)
        % load extra files
        wb.Message = sprintf('Loading %s',fileNames{i});
        [OK,nextSS] = loadSingleFile(fullFileNames{i},fileTypeValue,importFcns,data,Args{:});
        wb.Waitbar.Value = i;
        drawnow update
        OK = OK && ~wb.StopState;
        if OK 
            % append data
            try
                fileName = char(get(ss,'FileName'),get(nextSS,'FileName'));
                [ss,OK] = append(ss,nextSS,'strict');
                ss = set(ss, 'FileName', fileName);
            catch ME
                OK = false;
                break;
            end
        else
            break
        end
    end
    delete(wb);
end



function [OK,ss] = loadSingleFile(fileName,fileTypeValue,importFcns,data,varargin)
% load a single file
OK = false;
ss = sweepset;
[~, ~, ext] = fileparts(fileName);

if fileTypeValue == 1
    % Automatic detection of file type
    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)
        uiwait(errordlg(['Automatic file detection failed - unknown file type. '...
                'Please select appropriate file type from drop down list'], 'Data Loading Error',...
            'modal'));
        return
    end
    if length(found) > 1
        foundFirstStr = importFcns(found(1)).fileType;
        answer = questdlg(['Assuming file to be a ' foundFirstStr '. To select a '...
                'different file type use the drop down box'], 'Ambiguous Data File Type', ...
            'OK', 'Cancel', 'OK');
        if ~strcmpi(answer, 'ok')
            return
        end
    end
    fileTypeValue = found(1); 
else
    % Manual file type detection, subtract 1 from the Auto label
    fileTypeValue = fileTypeValue - 1;
end

try
    % call with GUI Mode true
    [OK, msg, data] = feval(importFcns(fileTypeValue).function, fileName, data, true,varargin{:});
catch E
    if strcmp(E.identifier,'MATLAB:TooManyInputs')
        try
            % old API without GuiMode
            [OK, msg, data] = feval(importFcns(fileTypeValue).function, fileName, data);
        catch E
            error(message('mbc:mbcmodel:data:LoadError', fileName, E.message));
        end
    else
        error(message('mbc:mbcmodel:data:LoadError', fileName, E.message));
    end
end

OK = OK && isSweepsetStruct(ss, data);
if OK
    % Check that sweepset structure is correct
    ss = struct2sweepset(sweepset, data);
    ss = set(ss, 'FileName', fileName);
end

if ~OK && ~isempty(msg)
    % only show the error dialog if not OK and there was a message 
    errordlg(msg,'Data Load Error','modal')
end