How to overwrite contents in a existing csv file?
38 次查看(过去 30 天)
显示 更早的评论
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
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 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!