从excel到matlab在多个字段中转换日期 [英] Converting date from excel to matlab in multiple fields

查看:257
本文介绍了从excel到matlab在多个字段中转换日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个matlab中有很多字段(gilt_name)的结构(gilts)。该结构是从excel导入的工作簿,每个字段对应于该工作簿中的工作表。每个工作表是一个时间序列的日期和相应的后备母猪的价格。根据何时从Excel文件转换日期。但是如何在matlab中同时将这个转换应用于多个字段呢?我有数百个数组的数据对应于每个gilt_name,它会节省我很多时间。
非常感谢。

解决方案

让我们说,我们有一个Excel电子表格,包含三张表,每个包含一系列的日期和一些相应的值:





考虑以下代码将数据导入到MATLAB中:

 %#的三张图纸
data = cell(3,1);
for i = 1:3
%#将表格读取为未处理的数据(文本和数字)
[〜,〜,raw] = xlsread('data.xlsx',i) ;

%#解析字符串作为串行日期并组合成矩阵
data {i} = [datenum(raw(:,1),'dd / mm / yyyy')cell2mat (:,2))];
end

现在单元格数组中的每个单元格 data 具有第i个工作表的数据。日期表示为序列号。例如,我们可以绘制第一个时间序列:

  plot(data {1}(:, 1),data {1 $($)
datetick('x','dd','keepticks')
xlabel('days'),ylabel('prices')


I have a structure (gilts) in matlab with many fields (gilt_name). The structure is an imported workbook from excel, and each field corresponds to a worksheet in that workbook. Each worksheet is a time series of dates and corresponding prices of gilts. I converted the dates in excel to strings before importing into matlab as instructed in When to Convert Dates from Excel Files. But how do I apply this conversion to many fields simultaneously in matlab? I have hundreds of arrays of data corresponding to each gilt_name and it would save me lot of time. Many thanks.

解决方案

Lets say we had an Excel spreadsheet containing three sheets, each contains a series of dates and some corresponding values:

Consider the following code to import the data into MATLAB:

%# for each of the three sheets
data = cell(3,1);
for i=1:3
    %# read the sheet as unprocessed data (both text and numeric)
    [~,~,raw] = xlsread('data.xlsx', i);

    %# parse strings as serial dates and combine into a matrix
    data{i} = [datenum(raw(:,1),'dd/mm/yyyy') cell2mat(raw(:,2))];
end

Now each cell in the cell array data has the data of the i-th sheet. Dates are represented as serial date numbers. For example we can plot the first time series as:

plot(data{1}(:,1), data{1}(:,2))
datetick('x', 'dd', 'keepticks')
xlabel('days'), ylabel('prices')

这篇关于从excel到matlab在多个字段中转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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