2010-06-23 40 views
-1

How can I merge this data in MATLAB?MATLAB创建新列

我的问题与上面的链接有关。 使用下面的代码(谢谢gnovice),它将创建一个带有3列的新文件(覆盖列time)。相反,覆盖柱time我想添加修改time作为新column..which使得新文件= 4列[a time c modifiedTime].

a = [1; 2; 3; 4; 5];   %# Sample data 
time = [10; 40; 20; 11; 40]; %# Sample data 
c = [0; 1; 0; 0; 1];   %# Sample data 

index = find(c == 1);    %# Find indices where c equals 1 
temp = time(index);     %# Temporarily store the time values 
time(index) = 0;      %# Zero-out the time points 
time(index-1) = time(index-1)+temp; %# Add to the previous time points 
c(index) = 0;      %# Zero-out the entries of c 

fid = fopen('newData.txt','wt');    %# Open the file 
fprintf(fid,'%g\t %g\t %g\n',[a time c].'); %'# Write the data to the file 
fclose(fid);         %# Close the file 

回答

1

我认为解决方案是增加另一个向量的fprintf中输出矩阵一样简单。我在顶部添加了新的modifiedtime矢量,并添加了如何使用fprintf语句输出数据。

a = [1; 2; 3; 4; 5];   %# Sample data 
time = [10; 40; 20; 11; 40]; %# Sample data 
c = [0; 1; 0; 0; 1];   %# Sample data 
modifiedtime = [3, 4, 7, 1, 2]; %# new array 

index = find(c == 1);    %# Find indices where c equals 1 
temp = time(index);     %# Temporarily store the time values 
time(index) = 0;      %# Zero-out the time points 
time(index-1) = time(index-1)+temp; %# Add to the previous time points 
c(index) = 0;      %# Zero-out the entries of c 

fid = fopen('newData.txt','wt');    %# Open the file 
fprintf(fid,'%g\t %g\t %g\t %g\n',[a time c modifiedtime].'); %'# Write the data to the file 
fclose(fid);  
+0

感谢瑞恩......但修改的时间,走的是列“时间” ......我的意思是modifiedTime不修改“时间” – Jessy 2010-06-23 11:14:32

+0

的过程之前存在的过程,我不完全确定你在问什么。你是不是只是想将一列数据添加到包含“修改时间”数据的输出文件中?如果你可以扩展你的原始问题多一点可能会有所帮助。 – Ryan 2010-06-23 18:40:16