Assuming your data is stored in a file named "data.csv" and it is located in your current MATLAB path, you can follow these steps:
- Read the data from the file into a matrix using ""readmatrix" function of MATLAB into a variable say "data".
- Create two new columns. The first column, named "label", should contain the string "Grid" repeated for each row of "data" and the second column should contain serial numbers from 1 to the number of rows in "data".
- Combine these new columns with the original data to form a new matrix.
- Write the updated matrix back to disk using "writematrix" function of MATLAB.
Kindly refer to the code section below:
% Read the original data from 'data.csv'
data = readmatrix('data.csv');
% Create the new columns
labels = repmat("Grid", size(data,1), 1); % Column of "Grid"
indices = (1:size(data,1))'; % Column of row numbers
% Combine all columns
data = [labels, num2cell(indices), num2cell(data)];
% Write to a new CSV file
writematrix(data, 'data.csv');
You can read more about "readmatrix" and "writematrix" function of MATLAB by using the following commands in the command window of MATLAB:
- doc readmatrix
- doc writematrix
I hope this helps!