MATLAB创建一个新列

时间:2010-06-23 10:49:47

标签: matlab

How can I merge this data in MATLAB?

我的问题与上述链接有关。 使用下面的代码(感谢gnovice),它将创建一个包含3列的新文件(覆盖列time)。我没有覆盖列time,而是将修改后的time添加为新列...这使得新文件= 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 个答案:

答案 0 :(得分: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);