How to use an excel sheet as function?

18 ビュー (過去 30 日間)
Teunis
Teunis 2023 年 9 月 18 日
編集済み: dpb 2023 年 9 月 20 日
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

採用された回答

dpb
dpb 2023 年 9 月 18 日
移動済み: dpb 2023 年 9 月 18 日
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 件のコメント
Teunis
Teunis 2023 年 9 月 19 日
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 2023 年 9 月 19 日
編集済み: dpb 2023 年 9 月 20 日
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...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by