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 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
