1

最初の列に日時が記載された次のテキストファイルがあります。ファイルは次のとおりです。

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が、成功しませんでした。何か助けはありますか?

前もって感謝します

4

2 に答える 2

1

文字列を手動で読み取る必要があります。

fid = fopen('yourfile.txt','r');
aux_cell  = textscan(fid,'%s');
date_cell = aux_cell{1}(1:4:end);
fclose(fid); 
于 2013-03-25T10:18:56.040 に答える
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
于 2013-03-25T10:58:14.263 に答える