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 日

0 投票

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 件のコメント

Pierre
Pierre 2019 年 6 月 5 日
編集済み: Pierre 2019 年 6 月 5 日
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
Bob Thompson 2019 年 6 月 5 日
You have NaNs in your csv file? Try using readtable instead. If that does not work then you might need to try textscan.
Pierre
Pierre 2019 年 6 月 5 日
Yes I have.
Ok thank you
Pierre
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
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
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.
error.PNG
Bob Thompson
Bob Thompson 2019 年 6 月 6 日
Convert B to a table. array2table should work
Pierre
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
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
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
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 件)

カテゴリ

質問済み:

2019 年 6 月 5 日

コメント済み:

2019 年 6 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by