MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中导入和修改日期数据 (https://www.labfans.com/bbs/showthread.php?t=23451)

poster 2019-12-10 20:41

在MATLAB中导入和修改日期数据
 
我有一个.csv文件,其中记录以以下形式编写:

2010-04-20 15:15:00,"8.9915176259e+00","8.8562623697e+00" 2010-04-20 15:30:00,"8.5718021723e+00","8.6633827160e+00" 2010-04-20 15:45:00,"8.4484844117e+00","8.4336586330e+00" 2010-04-20 16:00:00,"1.1106980342e+01","8.4333062208e+00" 2010-04-20 16:15:00,"9.0643470589e+00","8.6885660103e+00" 2010-04-20 16:30:00,"8.2133517943e+00","8.2677822671e+00" 2010-04-20 16:45:00,"8.2499419380e+00","8.1523501983e+00" 2010-04-20 17:00:00,"8.2948492278e+00","8.2884797924e+00" 根据这些数据,我想进行聚类-我想添加一列,其中带有表示小时的数字-因此,在第一行的情况下,必须在新行中添加值15。

第一个问题是调用函数

[numData, textData, rawData] = xlsread('testData.csv') 创建一个空矩阵numData以及一列textData和rawData结构。

是否可以创建任何模板来识别上述数据中的yyyy,MM,dd,hh,mm,ss值?

我基本上想对这些数据进行处理的是按小时对值进行分类,因此请从输入示例行中进行:

2010-04-20 15:15:00,"8.9915176259e+00","8.8562623697e+00" [B]更新1:在Matlab中,以上行被识别为字符串:[/B]
[INDENT] '2010-04-26 13:00:00,“ 1.0428104753e + 00”,“ 2.3456394130e + 00”'

[/INDENT]我希望这是输出:

15, 8.9915176259e+00, 8.8562623697e+00 [B]更新1:必须解析一个字符串[/B]

有谁知道如何解析字符串并从中获取时间戳('2010-04-20 15:15:00'),value1(1.0428104753e + 00)和value2(2.3456394130e + 00)作为单独的值?



[B]回答:[/B]

如果我对您的样本文件数据使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlsread.html"]XLSREAD[/URL] ,那么我将得到与[URL="https://stackoverflow.com/questions/2874359/import-modify-date-data-in-matlab/2874585#2874585"]yuk一样[/URL]的正确输出。 [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlsread.html"]XLSREAD[/URL]文档中的以下[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlsread.html"]语句[/URL]可能解释了您遇到的问题:
[INDENT]如果您的系统没有安装用于Windows的Excel,或者MATLAB无法访问COM服务器,则xlsread在基本模式下运行。在这种模式下, xlsread仅读取XLS文件。

[/INDENT]另外,我还可以使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/textscan.html"]TEXTSCAN[/URL]读取示例数据文件:

>> fid = fopen('testData.csv','r'); %# Open the file >> data = textscan(fid,'%s %s %s','Delimiter',',',... %# Read the data 'CollectOutput',true); >> fclose(fid); %# Close the file >> data = strrep(data{1},'"','') %# Format the data and remove '"' data = '2010-04-20 15:15:00' '8.9915176259e+00' '8.8562623697e+00' '2010-04-20 15:30:00' '8.5718021723e+00' '8.6633827160e+00' '2010-04-20 15:45:00' '8.4484844117e+00' '8.4336586330e+00' '2010-04-20 16:00:00' '1.1106980342e+01' '8.4333062208e+00' '2010-04-20 16:15:00' '9.0643470589e+00' '8.6885660103e+00' '2010-04-20 16:30:00' '8.2133517943e+00' '8.2677822671e+00' '2010-04-20 16:45:00' '8.2499419380e+00' '8.1523501983e+00' '2010-04-20 17:00:00' '8.2948492278e+00' '8.2884797924e+00' 现在,您可以使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/datevec.html"]DATEVEC[/URL] , [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cellfun.html"]CELLFUN[/URL]和[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/str2num.html"]STR2NUM[/URL]将单元格数组data的字符串格式化为数值矩阵:

>> dateVectors = datevec(data(:,1)); %# Format the date string >> newData = [dateVectors(:,4) cellfun(@str2num,(data(:,2:3)))] newData = %# A numeric matrix 15.0000 8.9915 8.8563 15.0000 8.5718 8.6634 15.0000 8.4485 8.4337 16.0000 11.1070 8.4333 16.0000 9.0643 8.6886 16.0000 8.2134 8.2678 16.0000 8.2499 8.1524 17.0000 8.2948 8.2885

[url=https://stackoverflow.com/questions/2874359]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 14:18

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.