Matlab的几个Excel文件 [英] matlab several excel files

查看:98
本文介绍了Matlab的几个Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个文件夹,每个文件夹包含几个(> 10)excel电子表格.如何将每个电子表格的最后一个工作表提取到matlab中.因此在matlab中,数据将是这样的,一个单元格将代表一个文件夹,而在该单元格中,您将拥有每个文件夹的内容,即每个电子表格的最后一个工作表中的数据?

欢呼

解决方案

使用 XLSFINFO 函数.它确定该文件是Excel电子表格,将为您提供其中所有工作表的列表,还可以检测excel文件格式.

[status,sheets,format] = xlsfinfo(filename);
if ~isempty(status)
    lastsheet = sheets{end};
end

一旦知道了工作表的姓氏,就可以使用 XLSREAD 来从中获取数据.

[num,txt,raw] = xlsread(filename,lastsheet);

使用 DIR 来收集文件夹中的文件名. /p>


更新

检查此脚本:

folders = {'test1','test2'};
DATA = cell(numel(folders),1);

for fo = 1:numel(folders)
    files = dir(folders{fo}); %# get array fo all files in a folder
    files([files(:).isdir]) = []; %# remove directories from the structure array
    DATA{fo} = cell(numel(files),1);
    for fi = 1:numel(files)
        filename = fullfile(folders{fo},files(fi).name);
        disp(filename)
        [status,sheets] = xlsfinfo(filename);
        if ~isempty(status)
            lastsheet = sheets{end};
            [num,txt,raw] = xlsread(filename,lastsheet);
            DATA{fo}{fi} = num; %# or txt, or raw
        end
    end
end

I have 4 folders each consisting of several (>10) excel spreadsheets. How is it possible to extract the last worksheet of each spreadsheet into matlab. So that in matlab, the data will be such that a cell would represent a folder and within that cell you would have the contents of each folder i.e. the data from the last worksheet of each spreadsheet?

cheers

解决方案

Use XLSFINFO function. It determines that the file is Excel spreadsheet, will give you the list of all worksheets in it, and also can detect the excel file format.

[status,sheets,format] = xlsfinfo(filename);
if ~isempty(status)
    lastsheet = sheets{end};
end

Once you know the last sheet name you can use XLSREAD to get the data from it.

[num,txt,raw] = xlsread(filename,lastsheet);

Use DIR to collect the file names in your folders.


UPDATE

Check this script:

folders = {'test1','test2'};
DATA = cell(numel(folders),1);

for fo = 1:numel(folders)
    files = dir(folders{fo}); %# get array fo all files in a folder
    files([files(:).isdir]) = []; %# remove directories from the structure array
    DATA{fo} = cell(numel(files),1);
    for fi = 1:numel(files)
        filename = fullfile(folders{fo},files(fi).name);
        disp(filename)
        [status,sheets] = xlsfinfo(filename);
        if ~isempty(status)
            lastsheet = sheets{end};
            [num,txt,raw] = xlsread(filename,lastsheet);
            DATA{fo}{fi} = num; %# or txt, or raw
        end
    end
end

这篇关于Matlab的几个Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆