Running m file from VBA macro and getting results in excel

7 次查看(过去 30 天)
hello everyone,
i want to interface excel and matlab to calculate condition number. I have an optimization algorithm in VBA macro which will give input values for optimization. these input values will be transferred to matlab, converted to a matrix and condition number of that matrix is returned to excel.
i tried to run m file from vba, it runs but without any output.
if i run this code from matlab, xlswrite wont work because excel file is open.
Excel cell values are not constant, these values keep changing for 100 iterations. Can anyone help to write an m file which will read these changing cell values, perform function in matlab, and returns the answer to excel file?
any help will be highly obliged.

回答(1 个)

埃博拉酱
埃博拉酱 2024-10-28,14:36
First of all, you need to know that it is impossible for MATLAB to execute your VBA code, and it is impossible for Excel to execute MATLAB code. There is no point in you trying to run m files with VBA.
Secondly from your description, it seems that you are trying to exchange data between Excel and MATLAB in real time while the code is running. This will involve complex inter-process communication operations, and you may need to use named pipes, memory-mapped files, or TCP local loopback techniques. Either way, you can't expect an online volunteer to do it for you.
In addition to inter-process communication, you can also consider:
  • Rewrite your VBA code to MATLAB, and then all the calculations are done by MATLAB. Or conversely, rewrite the MATLAB code to VBA, and then all the calculations are done by Excel.
  • Break down your calculation steps into two parts. First, use VBA to complete the calculations that can be performed by VBA code, save the intermediate results to an xlsx file, and close Excel; then use MATLAB to read the file, calculate it, and write it out to the file.
  • Compile your VBA or MATLAB algorithms into a stand-alone exe or dll, and then call it with the other party.

产品

Community Treasure Hunt

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

Start Hunting!

Translated by