3

適切に構成された入力テキスト ファイルがあります。

START_PARAMETERS
C:\Users\admin\Desktop\Bladed_wind_generator\_wind
C:\Users\admin\Desktop\Bladed_wind_generator\reference_v_4_2.$PJ
END_PARAMETERS
---------------------------------------------------------------------------
START_DLC1-2
4 6 8 10 12 14 16 18 20 22 24 26 28 29
6
8192
600
END_DLC1-2
---------------------------------------------------------------------------
START_DLC6-1
44.8
30
8192
600
END_DLC6-1
---------------------------------------------------------------------------
START_DLC6-4
3 31 33 35
6
8192
600
END_DLC6-4
---------------------------------------------------------------------------
START_DLC7-2
2 4 6 8 10 12 14 16 18 20 22 24 
6
8192
600
END_DLC7-2
---------------------------------------------------------------------------

現時点では、次のように読んでいます。

clc,clear all,close all

f = fopen('inp.txt','rt');  % Read Input File
C = textscan(f, '%s', 'Delimiter', '\r\n');
C = C{1}; % Store Input File in a Cell
fclose(f);

次に、正規表現を使用して、(START_DLC/END_DLC) ブロックが出現するたびに読み取ります。

startIndx = regexp(C,'START_DLC','match');
endIndx = regexp(C,'END_DLC','match');

目的は、各 START_DLC/END_DLC ブロック間のテキストの内容を構造化セル (store_DLCs と呼ばれる) に格納することです。結果は次のようになります (例: DLC1-2):

DLC1-2
4 6 8 10 12 14 16 18 20 22 24 26 28 29
6
8192
600

DLC7-2まで続きます。

進め方のヒントを教えていただけませんか?

よろしくお願いします。

BR、フランチェスコ

4

1 に答える 1

2

これまでのコードは問題ありません。ただし、 と の計算を次のように少し変更startIndxendIndxます。

startIndx = find(~cellfun(@isempty, regexp(C, 'START_DLC', 'match')));
endIndx = find(~cellfun(@isempty, regexp(C, 'END_DLC', 'match')));

次のように、実際のインデックスを取得できるようにします(見やすくするために、ここではそれらを転置しました)。

startIndx =

     6    13    20    27


endIndx =

    11    18    25    32

入力の整合性をチェックするアサーションも追加します。

assert(all(size(startIndx) == size(endIndx)))

上記のように計算されたすべてのインデックスを使用して、データをセルに抽出することができます。

extract_dlc = @(n)({C{startIndx(n):endIndx(n) - 1}});
store_DLCs = arrayfun(extract_dlc, 1:numel(startIndx), 'UniformOutput', false)

そして、各セルの名前 (最初のエントリ) を「修正」するには、次のようにします。

fix_dlc_name = @(x){strrep(x{1}, 'START_', ''), x{2:end}};
store_DLCs = cellfun(fix_dlc_name, store_DLCs,  'UniformOutput', false);

このコードを入力例に適用すると、セルの 1 行 4 列のセル配列が生成されます。

store_DLCs =

    {'DLC1-2', '4 6 8 10 12 14 16 18 20 22 24 26 28 29', '6', '8192', '600'}  
    {'DLC6-1', '44.8', '30', '8192', '600'}   
    {'DLC6-4', '3 31 33 35', '6', '8192', '600'}   
    {'DLC7-2', '2 4 6 8 10 12 14 16 18 20 22 24', '6', '8192', '600'}
于 2012-09-03T13:07:47.053 に答える