Effacer les filtres
Effacer les filtres

How to overwrite contents in a existing csv file?

35 vues (au cours des 30 derniers jours)
linkin113116
linkin113116 le 3 Mar 2018
Commenté : linkin113116 le 22 Avr 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
  1 commentaire
Stephen23
Stephen23 le 19 Mar 2018
"What do I do?"
Use a .mat file.

Connectez-vous pour commenter.

Réponse acceptée

NISARGA G K
NISARGA G K le 19 Mar 2018
Modifié(e) : NISARGA G K le 19 Mar 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
  1 commentaire
linkin113116
linkin113116 le 22 Avr 2018
Thanks very much Nishaga, this really helpful!

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 19 Mar 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 commentaire
linkin113116
linkin113116 le 22 Avr 2018
Thanks very much for your answer. I found 'xlswrite()' function can perfectly work.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by