tags:

views:

27

answers:

1

http://stackoverflow.com/questions/3071558/how-can-i-merge-this-data-in-matlab

my question is related to the above link. With the code below (thanks gnovice), it will create a new file with 3 column (overwrite column time). Instead of overwrite column time I want to add the modified time as a new column..which makes the new file =4 columns [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
A: 

I believe the solutions is as simple as adding another vector to your fprintf output matrix. I've added the new, modifiedtime vector at the top as an example and added how to output the data with the fprintf statement.

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);      
Ryan
thanks ryan...but the modified time is taking the process of the column 'time'...I mean the modifiedTime was not exist before the process of modifying the 'time'
Jessy
I'm not exactly sure what you are asking for then. Are you not just wanting to add a column of data to your output file that contains 'modified time' data? If you can expand on your original question a little more it may help.
Ryan

related questions