【发布时间】:2017-05-22 11:07:57
【问题描述】:
我有一个这样的输入 .txt 文件
head1 head2 head3 head3
0.004 5.104175 -1.651492 0.074480
0.015 5.104175 -1.327670 0.087433
0.025 5.104175 -1.181950 0.093910
...
并且我想将第一行减去同一个文件中的所有后续行,即打印一个这样的.txt文件:
0 0 0 0
0.011 0 -0.323825 -0.012953
...
这是我的代码:
for i = 1:length(x) %read all the files contained in folder_inp
%%check file extensions
[pathstr,name,ext] = fileparts(x(i).name);
%%if it is a text file...
if strcmp('.txt',ext)
s = importdata(strcat(folder_inp,'\',x(i).name));
init = s.data(1,:);
for k=1:length(s.data)
if s.data(k,:) == init
s.data(k,:) = zeros(1,length(s.data(k,:)));
else
s.data(k,:) = s.data(k,:)-init;
end
end
fid = fopen( strcat(folder_out,'\',name,'.txt'), 'w' );
formatSpecs = '%20s %20s %20s %20s \r';
for j = 1:length(s.data)
if j == 1
fprintf(fid,formatSpecs,'head1','head2','head3','head4');
elseif j==2
fprintf(fid,'\n') ;
else
fprintf(fid,formatSpecs,s.data(j,1),s.data(j,2),s.data(j,3),s.data(j,4));
end
end
fclose(fid);
end
end
一切正常,除了代码打印空字符而不是 0 的事实。有什么建议吗?
【问题讨论】:
-
输入文件中的符号a、b、c等代表什么?我想你实际上是在减去数字?你能发布一个输入文件的工作示例吗?
-
是的,没错。我已经编辑了问题