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
Show older comments
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 Comment
madhan ravi
on 22 Jul 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.
Accepted Answer
More Answers (2)
dpb
on 22 Jul 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 Comments
madhan ravi
on 22 Jul 2018
Edited: madhan ravi
on 22 Jul 2018
Thank you @dpb, a lot learnt from your answer.
dpb
on 23 Jul 2018
glad to hear...that's what we're here for. :)
Image Analyst
on 23 Jul 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 Comments
madhan ravi
on 23 Jul 2018
@Image Analyst Sir I’m sorry if this is irrelevant but I have been checking out your profile and I want to start learning Image Processing, if you could suggest some good sources to learn more about Image Processing i would be thankful :).
Image Analyst
on 23 Jul 2018
The Image Processing Handbook by John Russ is good. In addition the book by Eddins and Gonzales is good and uses MATLAB. There are also many, many demos and tutorials in the Mathworks documentation, and on my File Exchange especially my Image Segmentation Tutorial.
madhan ravi
on 23 Jul 2018
Thank you so much sir @Image Analyst highly appreciate your answer :)
Shreyansh Dubey
on 24 Jul 2018
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!