include calculated data into csv file
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
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',',');
Antworten (1)
Mathieu NOE
am 27 Okt. 2020
0 Stimmen
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 Kommentare
Mathieu NOE
am 28 Okt. 2020
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
Walter Roberson
am 28 Okt. 2020
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.
isamh
am 28 Okt. 2020
Walter Roberson
am 29 Okt. 2020
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')
isamh
am 29 Okt. 2020
Walter Roberson
am 29 Okt. 2020
%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')
isamh
am 29 Okt. 2020
Walter Roberson
am 30 Okt. 2020
%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')
Walter Roberson
am 30 Okt. 2020
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.
isamh
am 30 Okt. 2020
Walter Roberson
am 30 Okt. 2020
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))
isamh
am 30 Okt. 2020
Walter Roberson
am 30 Okt. 2020
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.
Walter Roberson
am 30 Okt. 2020
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.
isamh
am 30 Okt. 2020
Walter Roberson
am 30 Okt. 2020
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
isamh
am 30 Okt. 2020
Walter Roberson
am 30 Okt. 2020
Yes, you could use a different sheet, as long as your processing code knows to look at all appropriate sheets.
isamh
am 30 Okt. 2020
Walter Roberson
am 30 Okt. 2020
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.
Walter Roberson
am 30 Okt. 2020
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.
isamh
am 2 Nov. 2020
Walter Roberson
am 2 Nov. 2020
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.
isamh
am 2 Nov. 2020
isamh
am 2 Nov. 2020
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!