How can I write data to excel sheet in the next column automatically when my program runs for the 2nd time, 3rd time and so on while still having the previous runtime computations saved in previous columns of excel file
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Shreyansh Dubey
le 22 Juil 2018
Commenté : Image Analyst
le 25 Juil 2018
function []=calculate(a,b,c,d)
% code
calculation(1)=a*b;
calculation(2)=a+b+c+d;
calculation(3)=a*b*c*d;
calculation(4)=(a+b)-(c+d);
calculation = calculation'; %transpose of calculation array
excelfile= 'test.xlsx';
xlswrite(excelfile,calculation,'C4');
end
My actual code consists of a function which accepts 5 arguments and returns 14 values in an array similar to the code posted here. My problem is that each time I call the calculate() function it writes the data into test.xlsx file at column C starting from row4, but it overwrites the previously calculated data stored from C4. I want that when the 2nd time function calculate() is called the data should be written from column D and row4, for the third time to E4 and so on. Please help me with a solution.
1 commentaire
madhan ravi
le 22 Juil 2018
Yes because you have specified C4 column in the function? Maybe it’s better you change it each time while calling the function would be a better idea in my opinion.
Réponse acceptée
Shreyansh Dubey
le 25 Juil 2018
1 commentaire
Image Analyst
le 25 Juil 2018
Keep in mind that this is not general, like my solution, and only works if you keep track of which columns you're writing. If you open an existing spreadsheet, your code would overwrite existing cells unless you call xlsread() like I did to figure out where the existing data ends. But if you start with a new workbook, and keep track of everywhere you write, then this should be fine, and probably a little faster than using xlsread() every time.
Plus de réponses (2)
dpb
le 22 Juil 2018
Amplifying on madhan ravi's comment...
function []=storecalculate(a,b,c,d,excelfile,location)
...
xlswrite(excelfile,calculation,location);
remove the specifics from the routine--above shows one way (probably not the best factorization) is to pass the filename and the start location to the function and do the bookkeeping at the calling level to know which location is the desired one.
Perhaps(probably?) the better way would be to refactor on a more general level and vectorize to do all the computations and then write only once; xlswrite is a very high-overhead operation so calling it repetitively in a loop will result in noticeable latency in the application. If this only a couple columns or so, that probably won't be a big problem but if it's hundreds or thousands, you could have a long(ish) wait!
2 commentaires
madhan ravi
le 22 Juil 2018
Modifié(e) : madhan ravi
le 22 Juil 2018
Thank you @dpb, a lot learnt from your answer.
Image Analyst
le 23 Juil 2018
Right before you write an additional column, I'd read the existing workbook and get it's size
[numbers, strings, raw] = xlsread(filename);
Then compute how many columns are already in it
numColumns = size(raw, 2)
Then make a cell reference to one column past that, numColumns+1. You might need to use something like this routine https://www.mathworks.com/matlabcentral/fileexchange/15748-excel-column-number-to-column-name or excel2col() or similar, to turn column numbers into column letters, especially when you go past column Z. So do this:
excelColumnLetters = excel2col(excelColumnLetters);
cellReference = sprintf('%s1', excelColumnLetters); % Write to next column, row 1.
Then call xlswrite with that cell reference:
xlswrite(xlFileName, yourNewColumnData, sheetName, cellReference);
4 commentaires
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!