0

2 列のデータを含むテキスト ファイルがあります。このファイルを分割し、matlab に 2 つの個別の文字列として保存したいのですが、データ内の識別子に出会ったときにデータのコピーを停止し、2 つの新しい文字列を作成する必要もあります。

例えば

H3

7階

BB

タイ

スプリット

<>

SPLIT <> は、現在の文字列を終了する場所です。

fopen と fscanf を使用しようとしていますが、やりたいことを実行するのに苦労しています。

4

1 に答える 1

0

あなたが提供した例で次のスクリプトを試してみましたが、うまくいきました。コメントは非常に自明だと思います。

% Open text file.
fid = fopen('test.txt');

% Read the first line.
tline = fgetl(fid);

% Initialize counter.
ii = 1;

% Check for end string.
while ~strcmp(tline, 'SPLIT')

    % Analyze line only if it is not an empty one.
    if ~strcmp(tline, '')

        % Read the current line and split it into column 1 and column 2.
        [column1(ii), column2(ii)] = strread(tline, ['%c %c']);

        % Advance counter.
        ii = ii + 1;
    end

    % Read the next line.
    tline = fgetl(fid);
end

% Display results in console.
column1
column2

% Close text file.
fclose(fid);

ここでの重要な機能はfgetlstrreadです。彼らのドキュメントを見てください。いくつかの非常に素晴らしい例もあります。それが役に立てば幸い。

于 2013-10-21T11:00:04.450 に答える