matlab - matlab编辑文本文件,用他们的数字替换
问题描述
我想使用 Matlab 将文本文件中的每个浮点数替换为另一个数字。(假设是原始值的一半)其他数据(整数和字符串)不应更改。我的文本文件的几行(每个变量都在一个新行中):
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
任何帮助表示赞赏。
解决方案
这是使用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);
推荐阅读
- sql - 我在 DEV 和 PROD 中具有完全相同的代码,并且 PROD 上的少数属性(PII 数据)被屏蔽,而在 DEV 上则没有
- r - Power Bi 中 qicharts2 p 图表的问题:没有显示线,只有点
- java - 在循环中随机填充的值范围
- node.js - 我怎样才能弄清楚为什么 MongoDB NodeJS 驱动程序没有返回单个集合的有效查询的结果?
- php - 使用表单验证和操作更新 PHP 中的 Crud 操作
- vba - word用户窗体上的字母控制
- c++ - C++ try/catch 块不捕获给定类型的异常
- javascript - 无法搜索数组以返回按下的键码
- javascript - 为什么我的一个函数对每个 html 表格列起作用而另一个不起作用?
- c# - MOQ IConfiguration 与特定参数