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 (한국어)