【问题标题】:Working with Excel sheets in MATLAB在 MATLAB 中使用 Excel 工作表
【发布时间】:2015-12-10 18:58:13
【问题描述】:

我需要在 MATLAB 中导入一些 Excel 文件并对其进行处理。我的问题是每个 Excel 文件有 15 张纸,我不知道如何为每张纸“编号”,以便我可以制作一个循环或类似的东西(因为我需要在每张纸的某一列上找到平均值)。

我已经尝试导入数据并构建循环,但 MATLAB 将工作表注册为字符。

【问题讨论】:

  • 到目前为止你累了什么?可以发一下吗?
  • 我已经尝试导入数据(使用 variable= impordata(filename) 并且我得到了一个包含所有工作表等的结构。我找不到循环调用每个工作表的方法,因为工作表是以其名称而不是数字形式导入的(例如“sheet1、sheet2 等)。)
  • 好的,我会重新编辑帖子。谢谢
  • 好的,我做到了。给您带来的不便,我们深表歉意。

标签: excel matlab import


【解决方案1】:

使用xlsinfo 获取工作表名称,然后在循环中使用xlsread

[status,sheets,xlFormat] = xlsfinfo(filename);
for sheetindex=1:numel(sheets)
    [num,txt,raw]=xlsread(filename,sheets{sheetindex});
    data{sheetindex}=num; %keep for example the numeric data to process it later outside the loop.
end

【讨论】:

  • 非常感谢您的回答!它确实帮助我导入每张纸并对其编号,但我仍然遇到我不知道如何调用数据变量的特定列的问题。我实际上需要在每张纸上找到某一列的平均值(并存储在不同的表中)。所以当我使用这样的循环时: for i=1:15 a(i)=data{1, i} 我得到一个错误(在赋值 A(I) = B 中,B 和 I 中的元素数必须是一样。)对不起,如果我把你的时间浪费在愚蠢的事情上,但我已经被困了一段时间:p 再次感谢。
  • 看来a 是一个数组。数组的元素是标量(单个数字),您必须改用元胞数组。
【解决方案2】:

我只记得我在大约 2 年前发布了这个问题,自从我想通之后,我认为发布答案可能对将来的某人有用。 回顾一下;我需要从 4 个 excel 文件中导入一列,每个文件包含 15 个工作表。列的长度可变。我想出了两种方法来做到这一点。第一个是使用 xlsread 函数,语法如下。

for count_p = 1:2
    a = sprintf('control_group_%d.xls',count_p);
    [status,sheets,xlFormat] = xlsfinfo(a);

    for sheetindex=1:numel(sheets)
        [num,txt,raw]=xlsread(a,sheets{sheetindex},'','basic');
        data{sheetindex}=num;
        FifthCol{count_p,sheetindex} = (data{sheetindex}(:,5));

    end
end


  for count_p = 3:4
    a = sprintf('exercise_group_%d.xls',(count_p-2));
    [status,sheets,xlFormat] = xlsfinfo(a);

    for sheetindex=1:numel(sheets)
        [num,txt,raw]=xlsread(a,sheets{sheetindex},'','basic');
        data{sheetindex}=num;
        FifthCol{count_p,sheetindex} = (data{sheetindex}(:,5));
    end
end

显然命名为 control_group_1、control_group_2 等的文件。我在 xlsread 中使用了“基本”输入,因为我只需要文件中的原始数据,事实证明它比使用函数的全部功能要快得多。

导入数据的第二种方法,也是我最终使用的方法,是构建您自己的 activeX 服务器并在其上运行单个 excel 应用程序。 Xlsread 每次被调用时都会“打开”和“关闭”一个 activeX 服务器,所以它相当耗时(尽管使用“基本”输入不会)。我使用的代码如下。

 Folder=cd(pwd); %getting the working directory
d = dir('*.xls'); %finding the xls files
N_File=numel(d);  % Number of files

hexcel = actxserver ('Excel.Application'); %starting the activeX server
                                           %and running an Excel
                                           %Application on it
hexcel.DisplayAlerts = true;

for index = 1:N_File  %Looping through the workbooks(xls files)

    Wrkbk = hexcel.Workbooks.Open(fullfile(pwd, d(index).name)); %VBA 
                                                                 %functions                                    
    WorkName = Wrkbk.Name;  %getting the workbook name           %&commands
    display(WorkName)
    Sheets=Wrkbk.Sheets;    %sheets handle
    ShCo(index)=Wrkbk.Sheets.Count; %counting them for use in the next loop
    for j = 1:ShCo(index) %looping through each sheet

        itemm = hexcel.Sheets.Item(sprintf('sheet%d',j)); %VBA commands
        itemm.Activate;
        robj = itemm.Columns.End(4); %getting the column i needed

        numrows = robj.row;          %counting to the end of the column
        dat_range = ['E1:E' num2str(numrows)]; %data range
        rngObj = hexcel.Range(dat_range);
        xldat{index, j} = cell2mat(rngObj.Value); %getting the data in a cell
    end;
end
%invoke(hexcel);
Quit(hexcel);
delete(hexcel);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 2016-03-14
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多