How to use an excel sheet as function?

18 views (last 30 days)
Teunis
Teunis on 18 Sep 2023
Edited: dpb on 20 Sep 2023
I have an issues with a massive excel file. I have big excel model which i need to use for data processing. It could be seen as a black box function with a type unit load cases that are combined into one output.
With Matlab I want to send the variable to excel. Have excel calculate it. And then read it out. However the values are not updated in between and hence my output matrix has all the same rows.
How to make sure excel, updates it calculation in between?
While doing it manualy it works fine, but i want to do it for a n of 1000.....
n = 5; %amount of data poin
DC = linspace(0,1,n);
nameXLS ='File.xlsm';
Sheetname = "Calculation"
for i = 1:length(DC)
DCi = DC([i (length(DC)-i+1)])'; % variable prep to 2x1 colomn vector
writematrix(DCi,nameXLS,'Sheet',Sheetname,'range','C31:C32' ); % puts in variable in excel
A(i,:) = readmatrix(nameXLS,'Sheet',Sheetname ,'range','K86:P86' ) %get calcout
end

Accepted Answer

dpb
dpb on 18 Sep 2023
Moved: dpb on 18 Sep 2023
I'd suggest to move the calculations into MATLAB as being far more efficient; if you try to implement this in the fashion coded above, opening and closing the Excel workbook COM interface every time will become very expensive -- and you're already doing it twice in every loop; this will then be three times.
What do you consider "massive"? With today's amount of memory, I'd venture it's likely you could read the whole thing and do it all in memory with MATLAB vectorized code in a fraction of the time this will take.
  4 Comments
Teunis
Teunis on 19 Sep 2023
Thanks al lot! Much appreciated.
I'll have to plan some time for the steep learning curve. Your library will help.
For the short time we found an ugly work around in Excel by (mis)using the data table of what if analys as intermediate step. This reduces the amount of call to excel to 2.
dpb
dpb on 19 Sep 2023
Edited: dpb on 20 Sep 2023
Good luck...just one last note--my experience with the community college data was that when I initially tried reading/writing with read/writecell(), it was ok with only a few cells but when tried to do a large sheet that way it became exceedingly slow and eventually just hung the machine. At that point I had to revamp the whole process logic to instead be able to read/process the whole sheet instead of a cell at a time. Owing to the needs, that was somewhat more trouble to code but worked well in the end by using the library to fix up the sheet formatting and such after the base data had been written. An external template wasn't an option here because the formatting and size of the sections within a sheet is variable so the location of where specific formulas, etc., needed to be is variable as well.
The other "trick" that can help for this kind of manipulation is the <File Exchange xlswriteEx> submission that is a version of the venerable xlswrite that lets you write multiple times to the same workbook without closing the handle to the COM server every time as does the builtin (and as do the new replacement versions) since they're all built as standalone, independent functions to be used without any other conditions. Their performance isn't bad, but my experience indicates you can eventually have issues if need too many accesses inside tight loops...

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!