[英]matlab edit text file, replacing numbers by their
I want to use Matlab to replace every floating point number in a text file with another number. 我想使用Matlab将文本文件中的每个浮点数替换为另一个数字。 (let's say half the original value) Other data (integer and string) should not change. (假设是原始值的一半)其他数据(整数和字符串)不应更改。 A few lines of my text file (each variable is in a new line): 我的文本文件的几行(每个变量都在换行符中):
VERTEX
8
0
10
0.000000
20
110.500000
42
0.000000
0
VERTEX
8
0
10
0.000000
20
0.000000
42
0.000000
0
VERTEX
8
0
10
124.000000
20
0.000000
42
0.000000
0
VERTEX
8
0
10
248.000000
20
0.000000
42
0.000000
0
VERTEX
8
0
10
248.000000
20
110.500000
42
0.000000
0
VERTEX
8
0
10
248.000000
20
221.000000
42
0.000000
0
Any help is appreciated. 任何帮助表示赞赏。
Here is a solution using fgetl
and regexp
这是使用fgetl
和regexp
的解决方案
rid = fopen('test.txt','r');
wid = fopen('test2.txt','w');
while ~feof(rid)
s = fgetl(rid); % read a line
if regexp(s, '\d+\.\d+') % float founded
fprintf(wid, '42\n'); % wite "another integer"
else
fprintf(wid, '%s\n', s); % write original data
end
end
fclose(rid);
fclose(wid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.