Output of my matlab code gives out a string each time it is executed. I need to save the string in a excel sheet each time without overwriting the previous data in the excel sheet. The new string must be added in a new cell in the excel sheet.
3 Answers
The example:
filename = 'testdata.xlsx';
for i = 1:10
textdata = {['some text ', num2str(i)]};
writecell(textdata,filename,'Sheet',1,'Range',['B',num2str(i)]);
end
2 Comments
Anurag
in this code all the 10 output strings are generated at a time. i need to save the strings after each time my code is executed.
geoinformatic
In this code each string is generated iteratively. Just insert your code instead 'textdata = {['some text ', num2str(i)]}'
Daniels approach works with writecell, writetable, and writematrix, you just need to keep track of the range (or update the row indices of your range...)
There is a more complicated (or sophisticated) approach described here or here.
The question is if you
- really need to write there to the excel file consistently, or if your RAM allows to store everything in a matlab-table and write it once at the end to a file
- does it have to be an excel file, or is a .mat-file OK? (Which is easier to update)
- if you want to write to a comma-separated file, can you write to a .csv file? You can append a string (that can be a formatted line) to a text file, see here.
Option 3 is probably all you need if you don't want to change your original code.
dlmwrite('test.csv',MATRIX,'delimiter',',','-append');
