How to overwrite contents in a existing csv file?

38 ビュー (過去 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
  1 件のコメント
Stephen23
Stephen23 2018 年 3 月 19 日
"What do I do?"
Use a .mat file.

サインインしてコメントする。

採用された回答

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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by