Output new column to existing csv file with multiple columns

3 次查看(过去 30 天)
I have hundreds of csv files for which I'm running a function to shuffle the data in the 6th column and I need to output this shuffled data to a new column inside the existing file. All of the files currently have 6 columns so I need the shuffled data to be the 7th column. The csv files vary in length but all columns within the same file are the same length.
but I dont understand where to include the new column of data. Also it is from 2011 so perhaps there is an easier solution?
Thank you.
Here is a preview of one of the files:
Unnamed: 0 x y z unixTime mag
0 14272 -0.546875 1.484375 -0.1875 1506448016 1.5929838844916167
1 14273 -0.59375 1.25 -0.21875 1506448016 1.4010319857162434
2 14274 -0.40625 0.90625 -0.03125 1506448016 0.9936320684740404
3 14275 -0.296875 0.78125 0.046875 1506448016 0.8370684522486797
4 14276 -0.171875 0.671875 0.15625 1506448016 0.7108945728798328
5 14277 -0.140625 0.65625 0.1875 1506448016 0.6968469725305549
6 14278 -0.21875 0.578125 0.046875 1506448016 0.6199010757774179
I've been using dlmread to cut off the first row of strings (col headers) when I load in the data. Not sure why the values are shifted in the preview
  4 个评论

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-4-27
t = readtable('YourFile.csv');
t.newmag = t.mag(randperm(height(t))); %shuffled version of column 6
%any header that was not a valid variable name was modified and the original put
%into VariableDescriptions
hdr = t.Properties.VariableNames;
orig_hdr = t.Properties.VariableDescriptions;
mask = ~cellfun(@isempty, orig_hdr);
hdr(mask) = orig_hdr(mask);
hdr_str = strjoin(hdr, ',');
fid = fopen('NewFile.csv', 'wt');
fprintf(fid, '%s\n', hdr_str);
fprintf(fid, '%d,%d,%g,%g,%g,%g\n', t{:,:}.'); %relies on data being pure numeric
fclose(fid)

更多回答(1 个)

Ameer Hamza
Ameer Hamza 2018-4-27
Adding a column to a csv file is not as trivial as adding new rows. You will need to read complete file, change the data in MATLAB workspace and the re-write data to the csv file. Here is a general sketch of the code
data = csvread(filename);
% do processing on data, in your case shuffle 6th column and write it to 7th column of matrix
csvwrite(filename, changedData);

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by