我的文件类似于trial.txt
ReducedSize: True
Ref_Signal: Force
1 0,0000000000e+000 -3,14703e-003 0,00000e+000
2 1,0000000000e+000 2,27451e-001 4,66456e-002
123 6,3990000000e+003 3,76442e+001 -6,59653e+000
643 6,4000000000e+003 3,76920e+001 -6,61744e+000
Tagin:
Ovead: False
AveNumber: [] 1,00000e+001
我想删除所有以文本开头或者没有任何内容的行,然后将所有其余的数字数据放入一个4列的矩阵中,我所做的如下。
fid = fopen('trial.txt', 'r') ; % Open source file.
fgetl(fid) ; % Read/discard line.
fgetl(fid) ; % Read/discard line.
buffer = fread(fid, Inf) ; % Read rest of the file.
fclose(fid) % first two lines DELETED
fid = fopen('myFile_truncated.txt', 'w') ; % Open destination file.
fwrite(fid, buffer) ; % Save to file.
fclose(fid) ;
Data = fileread('myFile_truncated.txt');
Data = strrep(Data, ',', '.');
FID = fopen('myFile_truncated1.txt', 'w');
fwrite(FID, Data, 'char');
fclose(FID); % changed comma to points for data manipulation
dlmread('myFile_truncated1.txt') % convert the data to MARTRIX
然而,它工作正常,最后3行中的文字不会让它发生。任何人都可以建议如何删除第一行数和最后一行数?另外如何删除不包含文本的行,这样图表绘制会很容易?
我确实认为有更聪明的方法来做这些事情。
答案 0 :(得分:1)
这是我要做的事情
s = fileread('trial.txt'); % read file into string
s = regexprep(s,',','\.'); % replace any comma with period
s = regexprep(s,' +',' '); % replace any group of spaces with a single space
M = textscan(s,'%f %f %f %f',4,'HeaderLines',3); % read the numbers
M = [M{:}]; % concatenate all cell elements into a matrix