1

を含むtest.txtファイルがあります

bla bla bla
ble ble
BEGIN_SCAN
100 150
200 300
150 800
END_SCAN
blebleble
blublublu

このマトリックスのBEGIN_SCANとの間の数値のみを変更したいEND_SCAN

M = [100 125;
     200 350;
     150 835]

結果をresult.txtファイルに保存します。

何か案が?

4

3 に答える 3

2

fseekを使用してファイル内の特定の位置に移動し (もちろん最初に見つける必要があります)、次にfprintfを使用してその行の内容を上書きできます。

これは、たとえば、ファイルから行を削除する関数のexport_figで使用されます。eps_remove_background

于 2013-07-29T20:35:52.390 に答える
1

これはそれを行う必要があります:

txt = fileread('test.txt');
rows = regexp(txt,'\n','split');
[Lia, irows_begin] = ismember('BEGIN_SCAN', rows);
[Lia, irows_end] = ismember('END_SCAN', rows);
M_before = rows(irows_begin+1:irows_end-1);
M_before = cellfun(@(x) cell2mat(cellfun(@(y) str2num(y), strsplit(x, ' '), 'un', 0)), M_before, 'un', 0);
M_before = cell2mat(M_before(:));
M = [M_before(:,1) [125;350;835]];
M = num2cell(M, 2);
M = cellfun(@num2str, M, 'un', 0)';
rows = [rows(1:irows_begin) M rows(irows_end:end)];
fid = fopen('result.txt','wt');
fprintf(fid, '%s\n', rows{:});
fclose(fid);
于 2013-07-30T05:46:37.313 に答える
0

適切に動作するように @Will の回答から適応:

fid=fopen('test.txt');
txt =textscan(fid,'%s','delimiter','\n');
rows = txt{1,1};
irows_begin = find(ismember(rows,'BEGIN_SCAN'));
irows_end = find(ismember(rows,'END_SCAN'));
fclose(fid)

M_before = rows(irows_begin+1:irows_end-1);
M_before=cellfun(@str2num,M_before,'UniformOutput',false);
M_before = cell2mat(M_before(:));
M = [M_before(:,1) [125;350;835]];
M = num2cell(M, 2);
M = cellfun(@num2str, M, 'un', 0);
rows = [rows(1:irows_begin); M; rows(irows_end:end)];

fid = fopen('result.txt','wt');
fprintf(fid, '%s\n', rows{:});
fclose(fid);
于 2013-07-30T13:02:49.517 に答える