0

次のパラメータファイルがあり、ファイルの形式を変更せずに、左側の値を(に対して)gam.datまで変更したいと考えています。1 1 1-tail variable, head variable, variogram type

このパラメータファイルはループ内で呼び出されるため、ループを繰り返すたびにこのパラメータファイル内の値を変更する必要があります。

ファイルからの読み取りと書き込みは常に私の弱点でした。これを簡単に行う方法について何か助けはありますか?ありがとう!

                  Parameters
                  **********

START OF PARAMETERS:
gam.dat                         -file with data
1   1                           -   number of variables, column numbers
-1.0e21     1.0e21              -   trimming limits
gam.out                         -file for output
1                               -grid or realization number
100   1.0   1.0                 -nx, xmn, xsiz
100   1.0   1.0                 -ny, ymn, ysiz
 20   1.0   1.0                 -nz, zmn, zsiz
4  30                           -number of directions, number of h
1  0  1                         -ixd(1),iyd(1),izd(1)
1  0  2                         -ixd(2),iyd(2),izd(2)
1  0  3                         -ixd(3),iyd(3),izd(3)
1  1  1                         -ixd(4),iyd(4),izd(4)
1                               -standardize sill? (0=no, 1=yes)
1                               -number of gamma
1   1   1                       -tail variable, head variable, gamma type
4

1 に答える 1

1

このようなものが役立つかもしれません。それからまた、それはあなたが探しているものと正確に一致しないかもしれません。

fid = fopen(filename as a string);
n = 1;
textline = [];
while( ~feof(fid) ) // This just runs until the end of the file is reached.
    textline(n) = fgetl(fid)
    // some operations you want to perform?
    // You can also do anything you want to the lines here as you are reading them in.
    // This will read in every line in the file as well.
    n = n + 1;
end

fwrite(fid, textline);  // This writes to the file and will overwrite what is already there.
// You always write to a new file if you want to though!
fclose(fid);

ここでの使用を提案している唯一の理由はfgetl、行または行の情報に基づいて実行したい特定の操作/変更があるように見えるためです。これを使用freadして同じことを行うこともできますが、データを読み込んでマトリックスを構築するときに変更を加えるのではなく、構築後にマトリックス全体を操作する必要があります。

お役に立てば幸いです。

以下のコメントに基づくより完全な例。

fid = fopen('gam.txt');
n = 1;
textline = {};
while( ~feof(fid) ) % This just runs until the end of the file is reached.
textline(n,1) = {fgetl(fid)}
% some operations you want to perform?
% You can also do anything you want to the lines here as you are reading them in.
% This will read in every line in the file as well.

if ( n == 5 )  % This is just an operation that will adjust line number 5.
    temp = cell2mat(textline(n));
    textline(n,1) = {['newfile.name', temp(regexp(temp, '\s', 'once'):end)]};
end

n = n + 1;
end
fclose(fid)


fid = fopen('gam2.txt', 'w') % this file has to already be created.
for(n = 1:length(textline))
    fwrite(fid, cell2mat(textline(n));
end
fclose(fid)
于 2012-07-16T15:02:17.327 に答える