我在第一列中有以下带有datetime的文本文件。该文件是:
23/7/1998 20:00;" 1.8 "
23/7/1998 21:00;" 1.7 "
23/7/1998 22:00;" 2.2 "
23/7/1998 23:00;" 2.5 "
24/7/1998 0:00;" 2.2 "
24/7/1998 1:00;" 2.3 "
24/7/1998 2:00;" 2.4 "
24/7/1998 3:00;" 1.8 "
24/7/1998 4:00;" 1.9 "
我想将第一列(datetime)作为字符串。我尝试了函数datenum
,但没有成功。有什么帮助吗?
提前谢谢
答案 0 :(得分:1)
您需要手动阅读字符串:
fid = fopen('yourfile.txt','r');
aux_cell = textscan(fid,'%s');
date_cell = aux_cell{1}(1:4:end);
fclose(fid);
答案 1 :(得分:1)
您可以直接读取文件并将所需数据存储到单元格数组中,如下所示:
f_read = fopen('testcase.txt','r'); %The file 'testcase.txt' would contain the data of interest.
pat = '(?<datetime>[^;]*)(.*)'; %Regular expression to extract required field.
datetime = cell(0,1);
line = fgets(f_read);
while(line ~= -1) %Till the end of file.
line = regexp(line, pat, 'names'); %Extracts first column from input line.
datetime = [datetime;line.datetime]; %Append result as next row.
line = fgets(f_read);
end
对于您的输入,结果将是:
>> datetime
datetime =
'23/7/1998 20:00'
'23/7/1998 21:00'
'23/7/1998 22:00'
'23/7/1998 23:00'
'24/7/1998 0:00'
'24/7/1998 1:00'
'24/7/1998 2:00'
'24/7/1998 3:00'
'24/7/1998 4:00'
可以按如下方式访问每个单独的记录:
>> datetime{1}
ans =
23/7/1998 20:00