从excel到matlab在多个字段中转换日期 [英] Converting date from excel to matlab in multiple fields
问题描述
非常感谢。
让我们说,我们有一个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屋!