include calculated data into csv file

so, I created a code that does some calculaiotns for me, i have an .xlsx file with data and my code solves for some calcautions that i need. I want to add those calctions to the xlsx file and i found a way to do that. this time, instead of a xlsx file, i have a csv file. how can i do that with the csv file?
please try to help me, im stuck
code for xslx file( i want to keep data already on file and include the calculations)
%Table
Table1 = table(Speed_50_1 , Speed_50_2 , Speed_55_1 , Speed_55_2 , Speed_60_1 , Speed_60_2 , Speed_65_1 , Speed_65_2 , Speed_70);
S = Table1{:,:};
filename = 'Tesla_Steady_State_005.xlsx';
writetable(S,filename,'Sheet',1,'Range','A1:A27')
code for csv(this add the calculations but gets rid of all the data):
Table1 = table(Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70);
S = Table1{:,:};
writetable(S,'Tesla_Steady_State_027.csv')
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');

回答(1 个)

Mathieu NOE
Mathieu NOE 2020-10-27
hi
I think the solution already exist :
help writetable
"WriteMode" : - "append": Append to the bottom of the occupied range within the sheet.
this way you can add at he bottom of your file your new output

30 个评论

isamh
isamh 2020-10-27
编辑:isamh 2020-10-27
how would i use this for the given code? also, could i add it to the right of the data? appreciate all the help!
hi
would be helful if you could share your code with some input data and a template of how the output should be integrated into the csv file
The only way to add columns to a csv file is to rewrite the file completely, such as by reading in the current content, adding the new data, and writing it out again.
However if you just need to add new rows to a csv file then you can use 'Writemode', 'append' like Mathieu indicated.
If you are adding a lot of columns, then you should reconsider whether perhaps you should be writing your columns as rows and your rows as columns.
I have about 10 columns that are about 20,000 rows long.
so how should i add this into my code:
help writetable
"WriteMode" : - "append":
Table1 = table(Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70);
S = Table1{:,:};
writetable(S,'Tesla_Steady_State_027.csv')
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_preserve.Speed_73_2 = whatever %new data
writetable(T_preserve,'Tesla_Steady_State_027.csv')
You cannot use WriteMode append to add new columns, only new rows. If you were adding new rows for an existing table
%this should contain NEW data only!
Table1 = table(Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70);
writetable(Table1, 'Tesla_Steady_State_027.csv', 'WriteVariableName', false, 'WriteMode', 'append')
wont work,
i have the 2019 version of matlab, how would i get it to work?
%this should contain NEW data only!
Table1 = table(Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70);
dlmwrite('Tesla_Steady_State_027.csv', Table1{:}, '-append')
% i got this error message:
Subscripting into
a table using one
subscript (as in
t(i)) or three or
more subscripts
(as in t(i,j,k))
is not supported.
Always specify a
row subscript and
a variable
subscript, as in
t(rows,vars).
also, this is the old data with new data, correct? what is the speed_73_2 part?
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_preserve.Speed_73_2 = whatever %new data
writetable(T_preserve,'Tesla_Steady_State_027.csv')
%this should contain NEW data only!
Table1 = table(Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70);
dlmwrite('Tesla_Steady_State_027.csv', Table1{:,:}, '-append')
what is the speed_73_2 part?
You asked could i add it to the right of the data?
Adding new data to the "right" of an existing table is done by assigning to new variable names in the table. The example I gave was for the case where the new column just happened to be named Speed_73_2 . Looking at the theme of your existing variables perhaps you would have
T_preserve.Speed_75_1 = whatever
T_preserve.Speed_75_2 = whatever
if you were wanting to add Speed_75_1 and Speed_75_2 as columns.
im sorry, i tried the code above, keeps giving me an error message.
Undefined
function 'real'
for input
arguments of
type 'table'.
Error in
dlmwrite (line
181)
str
=
sprintf('%.*g%+.*gi',precn,real(m(i,j)),precn,imag(m(i,j)));
After you do the
Table1 = table(Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70);
step, please do
test = Table1{:,:};
disp(class(test))
disp(class(Speed_50_1))
i did that and got this:
Undefined function 'real' for
input arguments of type 'table'.
Error in dlmwrite (line 181)
str =
sprintf('%.*g%+.*gi',precn,real(m(i,j)),precn,imag(m(i,j)));
could i possibly output those results in the same file but a different sheet? which would be easier to do?
Please show us the output of what I suggested before,
test = Table1{:,:};
disp(class(test))
disp(class(Speed_50_1))
These are not intended to fix the problem: these are to give us more information to know what to try next.
i inculded the file. it seems correct and when i run the code. it shows table twice on the command window
Table1 = table(Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70);
test = Table1{:,:};
disp(class(test))
disp(class(Speed_50_1))
table
table
Why are you creating a table() of tables? If you already have tables, why are you not doing something like
Table1 = [Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70];
dlmwrite('Tesla_Steady_State_027.csv', Table1{:,:}, 'WriteMode', 'append')
Reminder: this would be used only if you are adding new rows to exactly the same list of variables in the same order, and would not be used to add additional columns.
so, the column names are different comapred to what i want to add. would this now work? im stuck on this, i got my code to work on an excel file but i cant figure out the csv file
If the column names are different compared to what you want to add, then you want to add new columns, and as I pointed out repeatedly earlier, there is no way to use appendmode or anything similar to write new columns. Please see my discussion on this topic at https://www.mathworks.com/matlabcentral/answers/618298-writing-data-in-columns-from-for-loop-to-txt-file#answer_517523
so i would have to tranpose the table and add the data by extendting the # of rows. could i keep the data the same and enter the found data in a different sheet in the same file?
Yes, you could use a different sheet, as long as your processing code knows to look at all appropriate sheets.
could you help with that? so the csv file is only one sheet, how could i add a new sheet and include the data?
Oh, good point. csv files do not have "sheets" so you would need to use a different file.
Or you have to use the ideas I posted much earlier,
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_preserve.Speed_73_2 = whatever %new data
writetable(T_preserve,'Tesla_Steady_State_027.csv')
where you are reading the existing data as a table, adding new variables to the table, and writing out the complete table.
so the new data would have different variable names, every single one of them which is about 27 columns.
i just dont understand how to include that with this:
T_preserve.Speed_73_2 = whatever
should Speed_73_2 be Table1?
If you are adding on a complete set of values that is already a table() object then
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_new = [T_preserve, Table1];
writetable(T_new, 'Tesla_Steady_State_027.csv');
where Table1 is a table object containing the new columns.
thanks for that,
im getting this error now:
All tables in the bracketed
expression must have the same
number of rows.
You will have to pad the smaller one. The table-related functions do not support "holes"
Reminder that the csv format does not permit holes. Every row in csv file must have the same number of columns, even if you have to use empty columns.
so something like this?
Table1 = [Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70];
PD = padarray(Table1,[1,0],0)
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_new = [T_preserve, PD];
writetable(T_new, 'Tesla_Steady_State_027.csv');
%this is what i got
Error using padarray>ParseInputs
(line 131)
Function padarray expected A
(argument 1) to be numeric or
logical for constant padding.
Error in padarray (line 68)
[a, method, padSize, padVal,
direction] = ParseInputs(args{:});

此问题已关闭。

标签

提问:

2020-10-26

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by