Matlab:在文本文件中编辑值而不改变文件格式 [英] Matlab: Edit values in text file without changing the file format

查看:263
本文介绍了Matlab:在文本文件中编辑值而不改变文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的参数文件,我想在 gam.dat 到 1 1 (对 -tail变量,head变量,variogram类型),而不改变文件的格式。

这个参数文件将在循环内被调用,这样循环的每次迭代都需要改变这个参数文件中的值。

一个文件的读写一直是我的薄弱环节。任何关于如何轻松完成的帮助?

 参数
**********

START参数:
gam.dat - 包含数据的文件
1 1 - 变量个数,列数
-1.0e21 1.0e21 - 调整限制
gam.out输出文件
1 -grid或实现数
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 - 方向数,h
1 0 1 -xd(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 =否,1 =是)
1 - 伽玛数量
1 1 1-尾变量,头变量,伽马类型


解决方案

像这样的东西可能会有所帮助。然后又可能不是你正在寻找的。
$ b $ pre $ f $ fid = fopen(文件名作为字符串)
n = 1;
textline = [];
while(〜feof(fid))//这只是运行,直到到达文件的末尾。
textline(n)= fgetl(fid)
//你想要执行的一些操作?
//你也可以在你读的时候做任何你想要的东西。
//这个也会读取文件中的每一行。
n = n + 1;
结束

fwrite(fid,textline); //写入文件并覆盖已经存在的内容。
//如果你想要的话,你总是写一个新的文件!
fclose(fid);

我建议使用 fgetl 这里是因为它看起来像是根据行或行中的信息要做的具体操作/更改。你也可以使用 fread 来做同样的事情,但是在构建完成之后,你必须作为一个整体对整个矩阵进行操作,而不是在读取时对其进行修改数据在和构建矩阵。

希望有帮助!



更完整的示例基于下面的注释。

  fid = fopen('gam.txt'); 
n = 1;
textline = {};
while(〜feof(fid))%这只是运行,直到到达文件的末尾。
textline(n,1)= {fgetl(fid)}
%您要执行的一些操作?
%您也可以在阅读时按照您的意思做任何事情。
%这将读取文件中的每一行。

if(n == 5)%这只是一个调整行号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')%这个文件必须被创建。
for(n = 1:length(textline))
fwrite(fid,cell2mat(textline(n));
end
fclose(fid)


I have a following parameter file in which I want to change values on left hand side starting with gam.dat till 1 1 1 (against -tail variable, head variable, variogram type) without changing the format of the file.

This parameter file will be called inside the loop such that each iteration of the loop would require changing the values inside this parameter file.

Reading and writing from a file has always been my weak point. Any help on how this can be done easily? Thanks!

                  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

解决方案

Something like this might help. Then again it might not be exactly what you're looking for.

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);

The only reason I am suggesting the use of fgetl here is because it looks like there are specific operations/changes you want to make based on the line or the information in the line. You can also use fread which will do the same thing but you'll then have to operate on the matrix as a whole after it's built rather than making any modifications to it while reading the data in and building the matrix.

Hope that helps!

More complete example based on the comments below.

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)

这篇关于Matlab:在文本文件中编辑值而不改变文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆