필터 지우기
필터 지우기

Add a column in a CSV file

조회 수: 11 (최근 30일)
Pierre
Pierre 2019년 6월 5일
댓글: Pierre 2019년 6월 6일
Hello,
I would like to create a new column(between the column 4 and 5) with the same number for all rows (number 1 here).
Is it possible to do it with MATLAB and save the new file with the new column?
I think it's not complicated but I'm stuck, I tried with excel also, but it doesn't work as I want. Indeed, I would like to keep the same format (with the comma and not with semicolom)
Thank you for your help.
You can find attached the CSV file.
Many thanks,
MATLAB R2018b

채택된 답변

Bob Thompson
Bob Thompson 2019년 6월 5일
The best way to do this is to read the entire file in with csvread, add your column to the matrix, and then reprint the file with csvwrite.
A = csvread('9Entering_1.csv');
B = randi(10,size(A,2),1);
C = [A(:,1:4),B,A(:,5:end)];
csvwrite('9Entering_1_r2.csv');
  댓글 수: 12
Bob Thompson
Bob Thompson 2019년 6월 6일
Are you creating a loop to go through the different numeric values? If so you can just overwrite B with the desired integer.
for i = 2:4 % You need to make the B matrix first before doing this loop
B(:,:) = i;
end
If you are not looping your values, and just want to create a matrix you can do so by creating an appropriately sized matrix first, then changing the values to whatever integer you want.
B = zeros(size(A,1),1);
B(:,:) = 2;
Pierre
Pierre 2019년 6월 6일
Thanks, that's perfect.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by