Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MATLAB中使用Excel表格_Excel_Matlab_Import - Fatal编程技术网

在MATLAB中使用Excel表格

在MATLAB中使用Excel表格,excel,matlab,import,Excel,Matlab,Import,我需要在MATLAB中导入一些Excel文件并对其进行处理。我的问题是,每个Excel文件有15张工作表,我不知道如何对每张工作表进行“编号”,这样我就可以进行循环或类似操作(因为我需要找到每张工作表上某一列的平均值) 我已经尝试导入数据并构建一个循环,但MATLAB将表注册为字符。使用xlsinfo获取表名,然后在循环中使用xlsread [status,sheets,xlFormat] = xlsfinfo(filename); for sheetindex=1:numel(sheets)

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


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

使用
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

我刚刚记得我在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中使用了“basic”输入,因为我只需要文件中的原始数据,而且它比使用函数的全部功能要快得多

第二种导入数据的方法,也是我最后使用的方法,是构建自己的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);

到目前为止你累了什么?你能发布它吗?我已经尝试导入数据(使用variable=importdata(filename),我得到了一个包含所有工作表等的结构。我找不到一种方法来调用循环中的每个工作表,因为这些工作表是以名称而不是数字导入的(比如“sheet1、sheet2等”)好的,我会重新编辑这篇文章。谢谢,我这么做了。对于我可能造成的不便,我深表歉意。非常感谢你的回答!这确实帮助了我导入每张工作表并给它们编号,但我仍然遇到一个问题,我不知道如何调用数据变量的特定列。我实际上需要找到平均值(并存储在不同的表中)每个工作表上的某一列。因此,当我使用如下循环时:对于i=1:15a(i)=data{1,i},我得到一个错误(在赋值a(i)=B中,B和i中的元素数必须相同。)抱歉,如果我在愚蠢的事情上浪费你的时间,但我已经被困了一段时间:p再次感谢。似乎
a
是一个数组。数组的元素是标量(单个数字),你必须使用单元格数组。