Output new column to existing csv file with multiple columns
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Centauri Jolene
am 26 Apr. 2018
Beantwortet: Walter Roberson
am 27 Apr. 2018
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.
I tried using the code from this question: https://au.mathworks.com/matlabcentral/answers/23723-add-new-column-into-existing-text-file
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 27 Apr. 2018
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)
0 Kommentare
Weitere Antworten (1)
Ameer Hamza
am 27 Apr. 2018
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);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!