How to overwrite contents in a existing csv file?

조회 수: 19 (최근 30일)
linkin113116
linkin113116 2018년 3월 3일
댓글: linkin113116 2018년 4월 22일
Hey Guys I'm wondering how to overwrite contents to a specific cell in an existing csv file without overwrite other cells in the same worksheet. Say for example I have a very simple matrix in excel file. X1 X2 X3 1 3 5 X1,X2,X3 as inputs to a quadratic function with 3 variables. saying i need to modify the value of X1=1 in each iteration. I've tried dlmwrite, writetable() and csvwrite() but none of those function can do my thing without overwrite other cells which don't need to be changed. I just need one single cell in an existing csv file can be modified and any other places in this file won't be touched. What do I do? Thanks a lot guys

채택된 답변

NISARGA G K
NISARGA G K 2018년 3월 19일
편집: NISARGA G K 2018년 3월 19일
Hi! I understand you need to overwrite a single cell in an existing csv file without modifying values in other cell. You can do this using the 'xlswrite' function. I hope the following link would help you to do same. https://www.mathworks.com/matlabcentral/answers/229700-overwriting-a-vector-to-a-specific-location-into-csv-file
  댓글 수: 1
linkin113116
linkin113116 2018년 4월 22일
Thanks very much Nishaga, this really helpful!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2018년 3월 19일
This cannot work. Remember, that CSV files are stored in text format. When you have e.g. the file:
...
1, 2, 3
4, 5, 6
...
and want to "overwrite" the '1' by a '100' by inserting the characters, the file's contents becomes:
...
1002, 3
4, 5, 6
...
So you can overwrite values, if the new string is not longer than the old one and you care for the comma (or which separator is used) also. But as soon as the new string is longer than the old one, the the simplest solution is to import the complete file, modify the data in the memory and re-write the complete file. A little bit smarter is it to import the data at first until the location of the changes is found and to leave this initial part untouched. But you have to create your own function to find this byte. Therefore I suggest to use the first method.
If you have to modify many elements, the text format is a really bad choice. Using a binary format replacing a number is trivial and fast without the need to re-write the complete file.
  댓글 수: 1
linkin113116
linkin113116 2018년 4월 22일
Thanks very much for your answer. I found 'xlswrite()' function can perfectly work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by