matlab - 如何读取文本文件并在其中找到特定值?
问题描述
我有一个文本文件,它的每一行如下:
n:1 mse_avg:8.46 mse_y:12.69 mse_u:0.00 mse_v:0.00 psnr_avg:38.86 psnr_y:37.10 psnr_u:inf psnr_v:inf
n:2 mse_avg:12.20 mse_y:18.30 mse_u:0.00 mse_v:0.00 psnr_avg:37.27 psnr_y:35.51 psnr_u:inf psnr_v:inf
n:3 mse_avg:10.89 mse_y:16.33 mse_u:0.00 mse_v:0.00 psnr_avg:37.76 psnr_y:36.00 psnr_u:inf psnr_v:inf
n:4 mse_avg:12.45 mse_y:18.67 mse_u:0.00 mse_v:0.00 psnr_avg:37.18 psnr_y:35.42 psnr_u:inf psnr_v:inf
我需要在单独的行中读取每一行并使用readvars
matlab函数,但输出仅n
如下
n
n
n
n
它不能提取其他变量。你知道是什么问题吗?Matlab 还有其他读取文本文件的功能吗?
解决方案
您最初的问题询问仅提取n
和psnr_avg
. 我不确定这是意外还是有意编辑的。
如果情况仍然如此,请尝试对这些行进行文本扫描,然后提取n
并psnr_avg
使用命名的正则表达式标记:
fid = fopen('data.txt');
data = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);
M = regexp(data{1}, 'n:(?<n>\d+).*psnr_avg:(?<psnr_avg>[^\s]+)', 'names');
>> M{:}
ans =
struct with fields:
n: '1'
psnr_avg: '38.86'
ans =
struct with fields:
n: '2'
psnr_avg: '37.27'
ans =
struct with fields:
n: '3'
psnr_avg: '37.76'
ans =
struct with fields:
n: '4'
psnr_avg: '37.18'
推荐阅读
- firebase - Firebase如何检查路径包含值并允许读写其他路径
- javascript - 无效的 VNode 类型:未定义(未定义) - 与
- google-api - updateMask 可能仅包含“draftGrade”或“assignedGrade”在通过 Google 课堂 API 修补学生提交时出现 400 错误
- sql - 基于多个时间戳的 Oracle SUM 值
- laravel-backpack - 在背包中使用 softDelete 并在操作中停用
- javascript - 变量在局部函数中没有改变
- reporting-services - 如何在 RDLC 报告中制作两列并在右列添加页脚?
- python - 尝试下载完整的 HTML 页面
- c# - 无论如何要读取 Naduio 流而不是将其写入文件然后再次读取
- python - 从 Azure Function Python 运行时运行 ssh-keygen