在MATLAB中从CSV文件读取日期和时间 [英] Reading date and time from CSV file in MATLAB
问题描述
datetime,M01,M02,M03,M04,M05,M06
8/15/2009 0:00,5.8,7.8,7.8,7.3,0, b $ b 8/15/2009 0:10,7.1,8.1,8.1,7.7,0,8.1
8/15/2009 0:20,6.8,7.4,7.6,7.1,0,7.3
8/15/2009 0:30,5.6,6.8,7.1,6.6,0,6.8
8/15/2009 0:40,3.9,6.2,6.4,6.2,0,6.4
8 / 15/2009 0:50,4.6,5.5,6.1,5.8,0,5.6
8/15/2009 1:40,7,7,7.2,6.9,0,6.3
$你可以帮助我正确地读取这个CSV文件,所以我可以使用 //www.mathworks.com/access/helpdesk/help/techdoc/ref/datenum.htmlrel =noreferrer> datenum ?
csvread 当然不起作用。由于文字扫描我失去了如何呼叫它,因此我不会得到错误。
日期和时间在同一列。
对不起,这可能是一个愚蠢的问题!!!
我知道如何读取上面的文件。但是我怎么把它写回一个file.txt中完全相同的格式?
i意味着我修改了一些列,现在需要为第1列和第1行使用相同格式的类似文件。
感谢您的帮助
解决方案根据上面的注释,如果数据如下:
datetime,M01,M02,M03,M04,M05,M06
8/15/2009 0:00,5.8,7.8,7.8,7.3,0,7.9
8/15/2009 0:10 ,7.1,8.1,8.1,7.7,0,8.1
8/15/2009 0:20,6.8,7.4,7.6,7.1,0,7.3
8/15/2009 0:30,5.6 ,6.8,7.1,6.6,0,6.8
8/15/2009 0:40,3.9,6.2,6.4,6.2,0,6.4
8/15/2009 0:50,4.6,5.5 ,6.1,5.8,0,5.6
8/15/2009 1:40,7,7 7.2,6.9,0,6.3
然后使用以下内容将其读为矩阵:
fid = fopen file.csv','rt');
a = textscan(fid,'%s%f%f%f%f%f%f',...
'Delimiter',',','CollectOutput',1,'HeaderLines' ,1);
fclose(fid);
format short g
M = [datenum(a {1})a {2}]
...和输出I get:
M =
7.34e + 005 5.8 7.8 7.8 7.3 0 7.9
7.34e + 005 7.1 8.1 8.1 7.7 0 8.1
7.34e + 005 6.8 7.4 7.6 7.1 0 7.3
7.34e + 005 5.6 6.8 7.1 6.6 0 6.8
7.34e + 005 3.9 6.2 6.4 6.2 0 6.4
7.34e + 005 4.6 5.5 6.1 5.8 0 5.6
7.34e + 005 7 7 7.2 6.9 0 6.3 $如果您将显示设置为
< techdoc / ref / format.htmlrel =noreferrer> format 到一个长输出,你会看到完整的数字(注意它们仍然存储在完整),或使用 fprintf :
fprintf('%。9f \\\
',M(:,1))
734000.000000000
734000.006944445
734000.013888889
734000.020833333
734000.027777778
734000.034722222
734000.069444445
datetime, M01, M02, M03, M04, M05, M06
8/15/2009 0:00, 5.8, 7.8, 7.8, 7.3, 0, 7.9
8/15/2009 0:10, 7.1, 8.1, 8.1, 7.7, 0, 8.1
8/15/2009 0:20, 6.8, 7.4, 7.6, 7.1, 0, 7.3
8/15/2009 0:30, 5.6, 6.8, 7.1, 6.6, 0, 6.8
8/15/2009 0:40, 3.9, 6.2, 6.4, 6.2, 0, 6.4
8/15/2009 0:50, 4.6, 5.5, 6.1, 5.8, 0, 5.6
8/15/2009 1:40, 7, 7, 7.2, 6.9, 0, 6.3
Can you help me to read this CSV file properly so I can convert the first column into a string using datenum?
csvread of course does not work. With textscan I am lost how to call it , so I do not get errors.
The date and time are in the same column.
Sorry it might sound a dumb question!!!
I know how to read read the above file now. but how do i write it back in a file.txt in the exact same format??
i mean i modified some columns and now need a similar file with same format for column 1 and row 1.
Thanks for the help
解决方案 According to your comment above, if the data looks like:
datetime, M01, M02, M03, M04, M05, M06
8/15/2009 0:00, 5.8, 7.8, 7.8, 7.3, 0, 7.9
8/15/2009 0:10, 7.1, 8.1, 8.1, 7.7, 0, 8.1
8/15/2009 0:20, 6.8, 7.4, 7.6, 7.1, 0, 7.3
8/15/2009 0:30, 5.6, 6.8, 7.1, 6.6, 0, 6.8
8/15/2009 0:40, 3.9, 6.2, 6.4, 6.2, 0, 6.4
8/15/2009 0:50, 4.6, 5.5, 6.1, 5.8, 0, 5.6
8/15/2009 1:40, 7, 7 7.2, 6.9, 0, 6.3
then use the following to read it as a matrix:
fid = fopen('file.csv', 'rt');
a = textscan(fid, '%s %f %f %f %f %f %f', ...
'Delimiter',',', 'CollectOutput',1, 'HeaderLines',1);
fclose(fid);
format short g
M = [datenum(a{1}) a{2}]
... and the output I get:
M =
7.34e+005 5.8 7.8 7.8 7.3 0 7.9
7.34e+005 7.1 8.1 8.1 7.7 0 8.1
7.34e+005 6.8 7.4 7.6 7.1 0 7.3
7.34e+005 5.6 6.8 7.1 6.6 0 6.8
7.34e+005 3.9 6.2 6.4 6.2 0 6.4
7.34e+005 4.6 5.5 6.1 5.8 0 5.6
7.34e+005 7 7 7.2 6.9 0 6.3
if you set the display format to a long output, you will see the full numbers (note they are still stored in full), or use fprintf:
fprintf('%.9f\n', M(:,1))
734000.000000000
734000.006944445
734000.013888889
734000.020833333
734000.027777778
734000.034722222
734000.069444445
这篇关于在MATLAB中从CSV文件读取日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!