在Matlab上读取文本文件逗号分隔 [英] Reading Text file comma seperated on Matlab
本文介绍了在Matlab上读取文本文件逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试读取一个逗号分隔的文本文件,如下所示:
I'm trying to read a comma separated text file that looks like the following:
2017-10-24,01:17:38,2017-10-24,02:17:38,+1.76,L,Meters
2017-10-24,02:57:31,2017-10-24,03:57:31,+1.92,H,Meters
2017-10-24,05:53:35,2017-10-24,06:53:35,+1.00,L,Meters
2017-10-24,10:45:01,2017-10-24,11:45:01,+2.06,H,Meters
2017-10-24,13:27:16,2017-10-24,14:27:16,+1.78,L,Meters
2017-10-24,15:07:16,2017-10-24,16:07:16,+1.92,H,Meters
2017-10-24,18:12:08,2017-10-24,19:12:08,+0.98,L,Meters
到目前为止,我的代码是:
My code so far is:
LT_data = fopen('D:\Beach Erosion and Recovery\Bournemouth\Bournemouth Tidal Data\tidal_data_jtide.txt');% text file containing predicted low tide times
LT_celldata = textscan(LT_data,'%D %D %D %D %d ','delimiter',',')'
推荐答案
对于混合数据类型,我建议使用readtable
.这将直接将数据读入table
对象,而无需指定格式规范或使用fopen
,
For mixed data types, I'd recommend readtable
. This will read your data straight into a table
object without having to specify a format spec or use fopen
,
t = readtable( 'myFile.txt', 'ReadVariableNames', false, 'Delimiter', ',' );
然后您可以轻松地操作数据
Then you can easily manipulate the data
% Variable names in the table
t.Properties.VariableNames = {'Date1', 'Time1', 'Date2', 'Time2', 'Value', 'Dim', 'Units'};
% Create full datetime object columns from the date and time columns
t.DateTime1 = datetime( strcat(t.Date1,t.Time1), 'InputFormat', 'yyyy-MM-ddHH:mm:ss' );
如果您确实知道格式,则可以在readtable
中指定'format'
属性,它将在读取时转换数据.
If you do know the formats, you can specify the 'format'
property within readtable
and it will convert the data when reading.
这篇关于在Matlab上读取文本文件逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文