Info
This question is closed. Reopen it to edit or answer.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
include calculated data into csv file
1 view (last 30 days)
Show older comments
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',',');
0 Comments
Answers (1)
Mathieu NOE
on 27 Oct 2020
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 Comments
Mathieu NOE
on 28 Oct 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
on 28 Oct 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
on 28 Oct 2020
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',',');
Walter Roberson
on 29 Oct 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')
Walter Roberson
on 29 Oct 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
on 29 Oct 2020
Edited: isamh
on 29 Oct 2020
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')
Walter Roberson
on 30 Oct 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
on 30 Oct 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
on 30 Oct 2020
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)));
Walter Roberson
on 30 Oct 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))
Walter Roberson
on 30 Oct 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.
isamh
on 30 Oct 2020
Edited: isamh
on 30 Oct 2020
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
Walter Roberson
on 30 Oct 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.
Walter Roberson
on 30 Oct 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
Walter Roberson
on 30 Oct 2020
Yes, you could use a different sheet, as long as your processing code knows to look at all appropriate sheets.
Walter Roberson
on 30 Oct 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.
isamh
on 30 Oct 2020
Edited: isamh
on 30 Oct 2020
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?
Walter Roberson
on 30 Oct 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.
Walter Roberson
on 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
on 2 Nov 2020
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');
isamh
on 2 Nov 2020
%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{:});
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)