run excel macro from simulink

Hi All
I try to use MATLAB Function block to run Excel macro by using MATLAB Function block in Simulink. Below is my code:
function duty = fcn()
duty = zeros(1,1);
coder.extrinsic('actxserver')
excel = actxserver('Excel.Application')
excel.Workbooks.Open('D:\pso.xlsm')
excel.Run('Sheet1.run_pso');
excel.Quit;
end
However, error pop out as shown below:
Attempt to extract field 'Workbooks' from 'mxArray'.
Anyone can help me? Thank you.

3 件のコメント

Walter Roberson
Walter Roberson 2019 年 12 月 18 日
You would probably have to do something along the lines of
Workbooks = excel.Workbooks;
Workbooks.Open('D:\pso.xlsm');
However, it is possible that you would have to initialize Workbooks before doing that assignment, and initializing might be a problem in order to get the correct datatype.
Lau Boon Chong
Lau Boon Chong 2019 年 12 月 19 日
Hi, thank for answer my question. However, the problem still remain after modified the code as suggested. Anyway, how to initialize Workbooks?
Kavya Vuriti
Kavya Vuriti 2020 年 3 月 30 日
Hi,
The error given arises when there are variables which are not initialized and assigned directly. I think the following code must work:
Workbook = excel.Workbooks.Open('D:\pso.xlsm');
excel.Run('Sheet1.run_pso');
excel.Quit;
If the error persists, could you share the excel file with the macro.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

製品

リリース

R2019a

タグ

質問済み:

2019 年 12 月 18 日

コメント済み:

2020 年 3 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by