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

    function [OK, msg, out,Sheet] = xregReadExcel(filename, out,GuiMode,Sheet,excelCOM)
% XREGREADEXCEL Reads in an Excel file
%
% [OK, msg, out] = xregReadExcel(filename, out);
% [OK, msg, out] = xregReadExcel(filename, out,GuiMode);
%   specify whether the file reader is read from a GUI and can use extra
%   dialogs during the importing of data. The default is 
% [OK, msg, out] = xregReadExcel(filename, out,GuiMode,Sheet);
%   specify the name of the sheet from the command-line

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


if nargin<3
    GuiMode = true;
end
if nargin<5
    % Start the Excel interface
    excelCOM = xregExcel('start');
    closeExcel = onCleanup(@()xregExcel('close', excelCOM));
end
server = get(excelCOM.dataBook, 'application');
server.UserControl = false;   % User doesn't get to control this Excel
% Open the file filename
workbook = invoke(server.workbooks,'open',filename);
release(server);
% Get the worksheets array
worksheets = workbook.sheets;
% Iterate through worksheets getting names
List = cell(1,double(worksheets.count));
sheetsOK = false(size(List));
sheetsOut(1,length(List)) = out;
for i = 1:length(List)
    worksheet = worksheets.Item(i);
    List{i}= get(worksheet,'Name');
    
    % read each sheet and check
    [sheetsOut(i),sheetsOK(i)] = readSheet(out,excelCOM,worksheet);
    
    release(worksheet);
end

sheetsOut = sheetsOut(sheetsOK);
List = List(sheetsOK);
msg = '';
if isempty(List)
    OK = 0;
    msg = sprintf('There are no valid worksheets in %s',filename);
elseif GuiMode && length(List) > 1
    % Which sheet do we want
    [index, OK] = mv_listdlg('ListString',List,...
        'PromptString','Select Sheet',...
        'Name','Select Sheet',...
        'SelectionMode','multiple',...
        'InitialValue',1,...
        'ListSize',[200 240],...
        'fus',5,...
        'ffs',8,...
        'uh',25);
    if ~OK && ~isempty(index)
       msg = 'No sheet selected.'; 
    end
elseif nargin>3 && ~isempty(Sheet)
    index = find(strcmp(Sheet,List));
    if isempty(index)
        OK = false;
        msg = sprintf('The work sheet %s must exist in %s',Sheet,filename);
    else
        OK = true;
    end
elseif isscalar(List) 
    OK = 1;
    index = 1;
else
    OK = false;
    msg = sprintf('There is more than one sheet in the file %s',filename);
end

if OK
    if isscalar(index)
        Sheet = List{index};
        out = sheetsOut(index);
    else
        ss = struct2sweepset(sweepset,sheetsOut(index(1)));
        for i=2:length(index)
            nextSS = struct2sweepset(sweepset,sheetsOut(index(i)));
            [ss,OK] = append(ss,nextSS,'strict');
            if ~OK
                break
            end
        end
        if OK
            out = sweepset2struct(ss);
        end
        
    end

else
    out = [];
    Sheet = [];
end
%Close up nicely
release(worksheets);
% Sometimes Excel prompts you to save your changes
% we don't want that.
SaveChanges = false;
invoke(workbook, 'close', SaveChanges);

function [out,OK] = readSheet(out,excel,worksheet)
%readSheet read a worksheet

try
    currentRange = worksheet.UsedRange.CurrentRegion;
    if currentRange.Rows.Count>2
        % check that there is some data
        release(currentRange);
        [varNames, varUnits, data] = xregExcel('read', excel, worksheet);
        out.varNames = varNames;
        out.varUnits = varUnits;
        out.data     = data;
        OK = ~isempty(data);
    else
        release(currentRange);
        OK = false;
    end
catch
    out = [];
    OK = 0;
end