Add a column in a CSV file
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2019년 6월 5일
0 개 추천
댓글 수: 12
Thank you for your answer.
But I have an error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Labelling (line 7)
C = [A(:,1:4),B,A(:,5:18)];
Do you have an idea?
Thank you
Bob Thompson
2019년 6월 5일
Pierre
2019년 6월 5일
Yes I have.
Ok thank you
Pierre
2019년 6월 5일
I tried with readtable, I can read the file but I have this error:
All input arguments must be tables.
Thank you for your help.
Bob Thompson
2019년 6월 5일
Please copy and past the entire error message.
Is there anything else in your file besides the table? I see you posted a copy of it, but unfortunately I am unable to look at it.
Pierre
2019년 6월 6일
Is the entire error message. I think, it is because on the matrix A (my file) it is a table, but the matric B is a double and not a table. Then in the matrix C it is impossible to combine them.

Bob Thompson
2019년 6월 6일
Pierre
2019년 6월 6일
It works for that thank you. Last point, I would like to have only number 1 for all rows in my new column but it is not only this number. Do you have an idea?
The code:
A = readtable('9Entering_1.csv');
B = randi(10,size(A,1),1);
T = array2table(B);
C = [A(:,1:4),T,A(:,5:end)];
writetable(C,'9Entering_1_r2.csv');
Thank you very much for your help
Bob Thompson
2019년 6월 6일
Your B matrix is created using the randi function, which creates an array of random integers between 0 and the first input argument. If you just want an entire column of 1s then just change how you are generating B.
B = ones(size(A,1),1);
Pierre
2019년 6월 6일
Ok thank you, it works. But if I want to do it with the numbers 2, 3 and 4 what I need to insert instead of "ones" ?
This is my last question.
Thanks
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
2019년 6월 6일
Thanks, that's perfect.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
