0

我已经在我的电脑上安装了 Matlab r2010a

我需要使用函数 xlsread 从一个 *.xls 或 *.xlsx 文件中加载数据。这不是一个大挑战,问题是如何修改xlsread.m以获得一个标志(整数),它给出了加载过程的百分比?

非常感谢。

到目前为止,我做了这个:将一个步进变量计数到一半,然后调用xlsread这需要一点时间,在加载过程之后,49.5% 的计数器计数到最后。

不是最好的,但这就是我所拥有的

file = 'example.xls';

h = waitbar(0, ['Loading data from ' file], ...
            'Name', 'Loading',...
            'CreateCancelBtn',...
            'setappdata(gcbf, ''canceling'', 1)');

steps = 200;

for step = 1 : steps

    %# Check for Cancel button press

    if getappdata(h, 'canceling')
        okCancel = 1;
        break
    end

    pause(0.01);         %# Delay time for wait bar progres

    if step == steps/2
        [data, txt] = xlsread(file);
    end

    %# Process status report

    waitbar(step/steps, h, sprintf(['Loading data from file... %3.2f%%'], step*100/steps));
end

set(h, 'CloseRequestFcn', 'closereq')
close(h)
4

1 回答 1

1

XLSREAD 函数允许您指定要读取的单元格范围。因此,您可以分批读取数据(例如读取表的前 50 行,然后读取接下来的 50 行,依此类推),每次“递增”进度条。

请注意,这将比一次读取整个表格要慢,因为该函数必须与 Excel COM 建立连接,然后在每次调用它时将其拆除(有关解释和可能的解决方案,请参阅此帖子)。


编辑:

这是一个简单的例子:

%# path to excel file to read
fname = fullfile(pwd,'test.xls');

%# get number of rows/columns in table
Excel = actxserver('Excel.Application');    %# open Excel COM Server
wb = Excel.Workbooks.Open(fname, 0, true);  %# open XLS file for reading
sheet = Excel.Worksheets.get('item',1);     %# activate first sheet
numRows = sheet.Range('A1').End('xlDown').Row;
numCols = sheet.Range('A1').End('xlToRight').Column;
wb.Close(false);                            %# close XLS file
Excel.Quit();                               %# cleanup
Excel.delete();
clear Excel wb sheet;

%# read rows in steps, and show progress-bar
numSteps = 20;
idx = fix(linspace(0,numRows,numSteps+1));
num = cell(numSteps,1);
h = waitbar(0, 'Reading excel file');
for i=1:numSteps
    %# update progress-bar
    waitbar(i / numSteps);

    %# range of cells to read
    cellRange = sprintf('%c%d:%c%d', ...
        'A', idx(i)+1, char('A'+numCols-1), idx(i+1));
    num{i} = xlsread(fname, 1, cellRange);
end
close(h)

%# merge all cells into a matrix
num = cell2mat(num);

截屏

这假定 Excel 文件包含一个数字数据表。随意适应您自己的文件。

于 2012-06-21T06:25:35.550 回答