How to include a new fixed string ('Name') on header and numerical variable (subsequent rows) in a new column, at the beggining, of a existing .csv file?

1 Ansicht (letzte 30 Tage)
My data.csv file, contains a header with variable names with two different variable types (string and double), as follows:
Region,Num1,Num2,Num3...,
1 and 2,324,0.32054,0.98517,...,
1 or 2,558,0.76092,0.91255,...,
.
.
.
I want to create a new column with the Identification on the header (string ID), and in the subsequent rows the Identification Number (for all value rows), that it is the variable that I must pass the value. I want something like this:
ID,Region,Num1,Num2,Num3...,
1314,1 and 2,324,0.32054,0.98517,...,
1314,1 or 2,558,0.76092,0.91255,...,
.
.
.
How can I start solving this?

Antworten (1)

Voss
Voss am 1 Jul. 2022
% show the contents of data.csv
type data.csv
Region,Num1,Num2,Num3 1 and 2,324,0.32054,0.98517 1 or 2,558,0.76092,0.91255
% the IDs you want to include in the file:
IDs = [1314; 1314];
% read the file
C = readcell('data.csv');
% prepend the ID column
C = [['ID'; num2cell(IDs(:))] C];
% write the result
writecell(C,'data_new.csv');
% show the contents of data_new.csv
type data_new.csv
ID,Region,Num1,Num2,Num3 1314,1 and 2,324,0.32054,0.98517 1314,1 or 2,558,0.76092,0.91255

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by