Hi,
I am trying to run a function in Simulink model. I have the function working without any error if I call it in an m-script. However, if I add the same function in MATLAB function block, I have dimension errors.
Why don't I see these errors when I call it in m-script? Is there is a way to debug this?
Thanks

 채택된 답변

Abdolkarim Mohammadi
Abdolkarim Mohammadi 2020년 9월 24일
편집: Abdolkarim Mohammadi 2020년 9월 24일

0 개 추천

Unlike m-file functions, MATLAB function block does not apply automatic array expansion. The simplest way to overcome is to define your m-file function as an extrinsic function in the MATLAB function block.
function v = fcn (u) % MATLAB function block
coder.extrinsic ('MATLAB function name'); % m-file function

댓글 수: 6

Hi Abdolkarim Mohammadi-Bilani,
Thanks for your response.
This solution somewhat worked but I am trying to call to functions inside matlab function block as shown below.
I have all undefined variables error. p, q, r, u, v are available in workspace. a, b, c, x, y, z, a1, b1, c1, m, n are defined in initABC and runABC.
Am I missing something here?
function [a, b, c] = fnc() %Matlab function block
a = 0; b = 0; c = 0;
coder.extrinsic ('initABC');
[a, b, c, x, y, z] = initABC(p, q, r); %m-file function
coder.extrinsic ('runABC');
for i= 0:20
[a1, b1, c1, m, n] = runABC(u, v, x, y);%m-file function
end
a = a1;
b = b1;
c = c1;
end
You should include the coder.extrinsic command before all other commands. In addition, I think it is better to do all of the calculations of the initialization and the main loop in a single m-file.
function v = fcn (u) % MATLAB function block
coder.extrinsic ('initAndRunABC'); % m-file function
v = 0; % pre-initialization
v = initAndRunABC (u);
Rubeena Parveez
Rubeena Parveez 2020년 9월 24일
How do I access base workspace variables in MATLAB function block?
Create a Constant block and enter that variable's name in the Value field. Connect the Constant block to the input of the function block. If you need multiple inputs, you first define them in the function header. Then the appropriate input ports will appear on the block.
function v = fcn (u1, u2, u3) % MATLAB function block
Rubeena Parveez
Rubeena Parveez 2020년 9월 24일
Thank you, this works but I read that coder.extrinsic does not support code generation. Is there any other way to do this?
Abdolkarim Mohammadi
Abdolkarim Mohammadi 2020년 9월 24일
편집: Abdolkarim Mohammadi 2020년 9월 24일
As far as I know, there is no way. As emphasized by documentation and the top contributor of this forum.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품

릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by