从文件Matlab中读取格式规范的问题 [英] Issue with format specification while reading from file Matlab
本文介绍了从文件Matlab中读取格式规范的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
0,000E + 0 4,069E-2我有一个.dat文件,其中包含以下顺序的数据: -5,954E + 0 1,851E-2
我需要做的是用matlab读取这些数据然后以某种方式处理它。
这是我的代码:
path ='C:/ Users /用户/桌面/ file1.dat';
fileID = fopen(path,'r');
formatSpec ='%e';
A = fscanf(fileID,formatSpec);
fclose(fileID);
disp(A);
不幸的是,这行不通。我在做什么错了?
解决方案
用逗号替换逗号后,可以用 dlmread
function:
M = dlmread('filename','');
M
就是您想要的。
对于第一部分,替换一个字符,可以使用下面的代码:
%读取文件
fid = fopen('input.txt','r');
f = fread(fid,'* char')';
fclose(fid);
%替换char
f = strrep(f,',','。');
%写入另一个文件
fid = fopen('output.txt','w');
fprintf(fid,'%s',f);
fclose(fid);
I have a .dat file with a table containing data in following order:
0,000E+0 4,069E-2 -5,954E+0 1,851E-2
What I need to do is to read this data with matlab and then somehow handle it.
Here is my code:
path = 'C:/Users/user/Desktop/file1.dat';
fileID = fopen(path,'r');
formatSpec = '%e';
A = fscanf(fileID,formatSpec);
fclose(fileID);
disp(A);
Unfortunately, it doesn't work. What did I do wrong?
解决方案
After replacement of comma with dot in data you can read it using dlmread
function:
M = dlmread('filename', ' ');
M
is what you want.
For the first part, replacing a character, you can use the following code:
% read the file
fid = fopen('input.txt','r');
f=fread(fid,'*char')';
fclose(fid);
%replace the char
f = strrep(f,',','.');
% write into the another file
fid = fopen('output.txt','w');
fprintf(fid,'%s',f);
fclose(fid);
这篇关于从文件Matlab中读取格式规范的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文