0

次の形式でファイルを読み込もうとしています(ただし、長すぎるため、最初の繰り返しでもデータを切り取っています)。

1.00 'day' 2011-01-02
'Total Velocity Magnitude RC - Matrix' 'm/day'
    0.190189     0.279141     0.452853      0.61355     0.757833     0.884577 
    0.994502      1.08952      1.17203      1.24442      1.30872      1.36653 
     1.41897      1.46675      1.51035      1.55003      1.58595      1.61824

ここに完全なデータを含む実際のファイルをダウンロードします

これは、上記のファイルからデータを読み取るために使用しているコードです。

fid = fopen(file_name); % open the file

dotTXT_fileContents = textscan(fid,'%s','Delimiter','\n'); % read it as string ('%s') into one big array, row by row
dotTXT_fileContents = dotTXT_fileContents{1};
fclose(fid); %# don't forget to close the file again

%# find rows containing 'Total Velocity Magnitude RC - Matrix' 'm/day'
data_starts = strmatch('''Total Velocity Magnitude RC - Matrix'' ''m/day''',...
    dotTXT_fileContents); % data_starts contains the line numbers wherever 'Total Velocity Magnitude RC - Matrix' 'm/day' is found

ndata = length(data_starts); % total no. of data values will be equal to the corresponding no. of '**  K' read from the .txt file

%# loop through the file and read the numeric data
for w = 1:ndata-1
    %# read lines containing numbers
    tmp_str = dotTXT_fileContents(data_starts(w)+1:data_starts(w+1)-3); % stores the content from file dotTXT_fileContents of the rows following the row containing 'Total Velocity Magnitude RC - Matrix' 'm/day' in form of string
    %# convert strings to numbers
    tmp_str = tmp_str{:}; % store the content of the string which contains data in form of a character
    %# assign output
    data_matrix_grid_wise(w,:) = str2num(tmp_str); % convert the part of the character containing data into number
end

私のテキストファイルのデータのパターンのアイデアを与えるために、これらはコードからのいくつかの結果です:

data_starts =

           2
        1672
        3342
        5012
        6682
        8352
       10022

ndata =

     7

したがって、myには行data_matrix_grid_wiseが含まれている必要があり1672-2-2-1(for a new line)=1667ます。しかし、私は結果としてこれを取得しています:

data_matrix_grid_wise =

  Columns 1 through 2

   0.190189000000000   0.279141000000000
   0.423029000000000   0.616590000000000
   0.406297000000000   0.604505000000000
   0.259073000000000   0.381895000000000
   0.231265000000000   0.338288000000000
   0.237899000000000   0.348274000000000

  Columns 3 through 4

   0.452853000000000   0.613550000000000
   0.981086000000000   1.289920000000000
   0.996090000000000   1.373680000000000
   0.625792000000000   0.859638000000000
   0.547906000000000   0.743446000000000
   0.562903000000000   0.759652000000000

  Columns 5 through 6

   0.757833000000000   0.884577000000000
   1.534560000000000   1.714330000000000
   1.733690000000000   2.074690000000000
   1.078000000000000   1.277930000000000
   0.921371000000000   1.080570000000000
   0.934820000000000   1.087410000000000

私はどこが間違っていますか?最終的な結果では、要素ではなく要素でdata_matrix_grid_wise構成される必要があります。ありがとう。1000036

更新: data_starts(w)の直前の行に「日」の前の数値、つまり1、2、3などを含めるにはどうすればよいですか?私はこれをループ内で使用していますが、機能していないようです:

days_str = dotTXT_fileContents(data_starts(w)-1);
    days_str = days_str{1};
    days(w,:) = sscanf(days_str(w-1,:), '%d %*s %*s', [1, inf]);
4

2 に答える 2

1

問題は最後の2つのステートメントにあります。tmp_str{:}セル配列をコンマ区切りの文字列リストに変換します。このリストを単一の変数に割り当てると、最初の文字列のみが割り当てられます。したがって、tmp_strデータの最初の行のみが含まれるようになります。

最後の2行の代わりにできることは次のとおりです。

tmp_mat = cellfun(@str2num, tmp_str, 'uniformoutput',0);
data_matrix_grid_wise(w,:) = cell2mat(tmp_mat);

cell2matただし、すべての行に同じ数の列があるわけではないため、連結()で問題が発生します。それをどのように解決するかはあなた次第です。

于 2012-04-09T14:06:30.660 に答える
1

行の問題tmp_str=tmp_str {:}; Matlabは、charを処理するときに奇妙な動作をします。あなたのための簡単な解決策は、最後を次の2行に置き換えることです。

y = cell2mat( cellfun(@(z) sscanf(z,'%f'),tmp_str,'UniformOutput',false));
data_matrix_grid_wise(w,:) = y;
于 2012-04-09T14:15:24.317 に答える