Filter löschen
Filter löschen

How to overwrite contents in a existing csv file?

35 Ansichten (letzte 30 Tage)
linkin113116
linkin113116 am 3 Mär. 2018
Kommentiert: linkin113116 am 22 Apr. 2018
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

Akzeptierte Antwort

NISARGA G K
NISARGA G K am 19 Mär. 2018
Bearbeitet: NISARGA G K am 19 Mär. 2018
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

Weitere Antworten (1)

Jan
Jan am 19 Mär. 2018
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 Kommentar
linkin113116
linkin113116 am 22 Apr. 2018
Thanks very much for your answer. I found 'xlswrite()' function can perfectly work.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by