使用Matlab函数读取每行文本文件

1视图(过去30天)
纳迪亚·纳吉
纳迪亚·纳吉 2021年4月21日
评论: 纳迪亚·纳吉2021年4月21日
我有一个文本文件,每行内容如下:
n:1 mse_平均值:8.46 mse_y:12.69 mse_:0.00 mse_v:0.00 psnr_平均值:38.86 psnr_y:37.10 psnr_:inf psnr_v:inf
n:2 mse_平均值:12.20 mse_y:18.30 mse_:0.00 mse_v:0.00 psnr_平均值:37.27 psnr_y:35.51 psnr_:inf psnr_v:inf
n:3 mse_平均值:10.89 mse_y:16.33 mse_:0.00 mse_v:0.00 psnr_平均值:37.76 psnr_y:36.00 psnr_:inf psnr_v:inf
n:4 mse_平均值:12.45 mse_y:18.67 mse_:0.00 mse_v:0.00 psnr_平均值:37.18 psnr_y:35.42 psnr_:inf psnr_v:inf
我需要在单独的一行中读取每一行,并使用readvars matlab函数,但输出仅为'n',如下所示
N
N
N
N
它不能提取其他变量。你知道有什么问题吗?Matlab是否有其他读取文本文件的功能?我需要从每一行中提取psnr_y,为此,我想在不同的变量中提取每一行,比如readvars函数的输出。

公认的答案

史蒂芬
史蒂芬 2021年4月21日
选择={“分隔符”,{':',' '}};
fid=fopen(“data.txt”,“rt”);
nmc=nnz(fgetl(fid)==':');
弗雷温(fid);
fmt=repmat(“%s%f”,1,nmc);
tmp=文本扫描(fid、fmt、opt{:});
fclose(fid);
fnm=[tmp{,1:2:end}];
out=cell2struct(tmp(:,2:2:end),fnm(1,:),2)
出去=带字段的结构:
n:[4×1双精度]平均信噪比:[4×1双精度]平均信噪比:[4×1双精度]平均信噪比:[4×1双精度]平均信噪比:[4×1双精度]平均信噪比:[4×1双精度]平均信噪比:[4×1双精度]平均信噪比:[4×1双精度]平均信噪比:[4×1双精度]
出去
ans= 4×1
1 2 3 4
out.psnr_y
ans= 4×1
37.1000 35.5100 36.0000 35.4200

更多答案(0)

社区寻宝

在MATLAB Central中查找宝藏,了解社区如何帮助您!

开始打猎吧!