使用matlab将一个列文本文件添加到两个列文本文件中

时间:2014-03-20 16:38:56

标签: matlab

我是MATLAB的新手。

我有两个文本文件。一个有两列xy.txt(X和Y坐标)。它有80640分。

第二列有一列z.txt(根据坐标的z值)。第二个文本文件是我写的matlab程序的结果。该程序每10年产生一次z值。我正在模拟本世纪海平面上升的影响。

我想制作三列文本文件来绘制等高线图。

我想将该代码包含在我编写的主脚本中,以便我可以自动获取轮廓图。

我搜索了将近一天才找到合适的答案,但徒劳无功

谢谢

1 个答案:

答案 0 :(得分:0)

这可以给你你想要的东西:

%Open and get the relevant data from the two files
fid1 = fopen('xy.txt');
fid2 = fopen('z.txt');
s1 = textscan(fid1,'%s %s','delimiter','\t');
s2 = textscan(fid2,'%s','delimiter','\t');

%close files
fclose(fid1);
fclose(fid2);

%sort data into an easy to use cell array
s=[s1{1,1},s1{1,2},s2{1,1}];

%Create new file and set permission to write
fid = fopen('xyz.txt','w');

%Loop through cell array s and write to file using a tab delimited format spec.
for ind = 1:size(s,1)
    fprintf(fid,'%s\t%s\t%s\n',s{ind,:});
end
fclose(fid); %close file

这假设xy.txt和z.txt都具有相同的行数

相关问题