Info
此问题已关闭。 请重新打开它进行编辑或回答。
include calculated data into csv file
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
此问题已关闭。
回答(1 个)
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 个评论
Mathieu NOE
2020-10-28
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
2020-10-28
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
2020-10-28
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
2020-10-29
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
2020-10-29
%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
2020-10-29
% 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).
Walter Roberson
2020-10-30
%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
2020-10-30
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
2020-10-30
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
2020-10-30
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
2020-10-30
could i possibly output those results in the same file but a different sheet? which would be easier to do?
Walter Roberson
2020-10-30
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
2020-10-30
编辑:isamh
2020-10-30
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
2020-10-30
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
2020-10-30
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
Walter Roberson
2020-10-30
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
2020-10-30
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?
Walter Roberson
2020-10-30
Yes, you could use a different sheet, as long as your processing code knows to look at all appropriate sheets.
isamh
2020-10-30
could you help with that? so the csv file is only one sheet, how could i add a new sheet and include the data?
Walter Roberson
2020-10-30
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
2020-10-30
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
2020-11-2
thanks for that,
im getting this error now:
All tables in the bracketed
expression must have the same
number of rows.
Walter Roberson
2020-11-2
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
2020-11-2
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
2020-11-2
%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{:});
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)