如何访问 excel 单元格中的所有数据?我需要导入到 MATLAB

How do I access all the data in an excel cell? I need to import into MATLAB

我有一个 Excel 数据文件,其中一个单元格包含日期和时间。存储在单元格中的数据(也就是公式栏中的数据)是:

3/11/2016  5:27:36 PM

但是同一个单元格显示

2016/03/11 17:27:37.653

我需要访问毫秒数,但出于某种原因,信息的处理方式不同。 Excel 无法使用它,除非我将它复制到 Word 并将其粘贴回 Excel。如何将毫秒数输入 MATLAB?我将需要阅读许多这样的文件。

我的 MATLAB 代码仅包含:

for i=1:5
    traverse_file=sprintf('traverse_s4r%d.xlsx',i);
    [num,text,both{i}]=xlsread(traverse_file);
end

这是 Excel 个单元格的样子:

2016/03/11 17:27:19.213 0.000004
2016/03/11 17:27:35.813 -0.00002
2016/03/11 17:27:36.000 0.000015

这是单元格中包含的内容:

3/11/2016  5:27:19 PM 0.000004
3/11/2016  5:27:36 PM -0.00002
3/11/2016  5:27:36 PM 0.000015

谢谢。

doc datestr

如果以下方法不起作用。有很多支持以各种格式操作 dates/times。在想出一个定制的解决方案之前,先利用这些。

time = datestr(text, 'yyyy/mm/dd hh:MM:dd.fff')
[y,m,d,h,m,s] = datevec(time);
ms = s - floor(s);

默认情况下,xlsread 检索 date/time 字段作为根据当前系统区域设置格式化的字符串。问题是在大多数情况下,秒的小数部分不会显示,导入文件时会丢失。 the documentation of xlsread 末尾有一条注释对此进行了解释:

xlsread imports formatted dates as strings (such as '10/31/96'), except in basic mode and on computers without Excel for Windows.

Fortunately, you can pass a custom processing function in xlsread to get the raw unformatted values (this is only supported on Windows computers with Excel software installed). As @RonRosenfeld explained in the comments, Excel stores datetimes as serial numbers 表示自 00-January-1900 以来的小数天数:

Excel stores dates as sequential serial numbers so that they can be used in calculations. By default, January 1, 1900, is serial number 1, and January 1, 2008, is serial number 39448 because it is 39,448 days after January 1, 1900.

您可以使用 .Value2 属性.

从 COM/ActiveX 中获取未格式化的值

请注意,MATLAB 有自己的存储约定 serial date numbers (as the fractional number of days since 00-January-0000). So once we load the raw serial numbers from Excel, we need to convert that to the convention used by MATLAB. With the datetime class in MATLAB, the conversion is easy as shown here


现在我们已经解决了所有问题,下面是以完整 date/time 精度加载 Excel 文件的代码:

function t = loadMyExcelFile(fnameXLSX, sheet)
    % read Excel sheet using our custom processing function to get the
    % raw dates without any formatting
    if nargin<2, sheet = 1; end
    [~,~,~,custom] = xlsread(fnameXLSX, sheet, '', '', @CustomProcessFcn);

    % convert excel dates to MATLAB datetime, also customize how
    % the datetime values are displayed (with full precision)
    dt = datetime(cell2mat(custom(:,1)), 'ConvertFrom','excel', ...
        'Format','yyyy-MM-dd HH:mm:ss.SSSSSS');
    val = cell2mat(custom(:,2));

    % compute the milliseconds part alone
    ms = round(rem(dt.Second, 1) * 1000);

    % build and return results as a MATLAB table
    t = table(dt, val, ms);
end

function [data, V2] = CustomProcessFcn(data)
    V2 = data.Value2;
end

为了验证上述导入功能,我创建了示例 a Excel sheet,其中包含随机日期和值(如果您想继续,可以 download it here)。

(注意:我将日期的单元格格式更改为自定义 yyyy-mm-dd hh:mm:ss.000 以查看毫秒部分)。

您终于可以加载数据并根据需要对其进行操作了:

>> t = loadMyExcelFile('Book1.xlsx')
t = 
                dt                  val      ms 
    __________________________    _______    ___
    2016-03-28 23:00:25.100877    0.31472    101
    2016-03-29 18:58:28.578988    0.72052    579
    2016-03-30 10:19:04.318113     0.3475    318
    2016-03-31 10:00:26.065898    0.76088     66
    2016-04-01 14:19:13.908256    0.89324    908
    2016-04-02 04:29:42.858488    0.49078    858
    2016-04-03 07:12:32.249770    0.26928    250
    2016-04-04 16:48:25.073809    0.31616     74
    2016-04-05 08:51:05.228647    0.77366    229
    2016-04-06 21:38:29.768989     1.2386    769
    2016-04-07 06:55:49.555229    0.89617    555
    2016-04-08 01:13:40.028169     1.3668     28
    2016-04-09 23:38:56.049314     1.8239     49
    2016-04-10 04:13:09.258885     1.8093    259
    2016-04-11 09:40:38.799400     2.1096    799
    2016-04-12 03:37:27.442933     1.7515    443
    2016-04-13 12:20:01.502968     1.6732    503
    2016-04-14 18:15:25.406924      2.089    407
    2016-04-15 14:46:10.802325     2.3812    802
    2016-04-16 02:58:43.615177     2.8407    615

>> plot(t.dt, t.val)