Arrays and embedded MATLAB function in Simulink

4 次查看(过去 30 天)
Dear all,
I am using Simulink (Matlab R2010a) to simulate the behaviour of an energy system. In this system, m boxes are connected in parallel and can be disconnected with a switch. The number of boxes m is a mask parameter which is defined by the user at the beginning of the simulation. Inputs of my embedded MATLAB function are the number of boxes m, the inlet power P[1,m] and the temperature T[1,m] of each box. Calculated parameters (outputs) are the produced gas flow rate G[1,m] and the temperature variation dT[1,m] of each box. The output dT[1,m] is connected with an integrator, which is connected with the input T[1,m].
This is the code structure in the embedded MATLAB function:
function [G,dT] = Boxes(m,P,T)
G=zeros(1,m);
dT=zeros(1,m);
for i=1:
Pin=P(1,i);
Tout=T(1,i);
Gas=f(Pin,Tout);
dTout=h(Pin,Tout);
G(1,i)=Gas;
dT(1,i)=dTout;
end
end
This code works in Matlab, but the simulation does not work in Simulink. I get the following error message:
Data 'dT' (#146) is inferred as a variable size matrix, while its specified type is something else.
In other words, I should specify in the "Ports and Data Manager" that the parameters P, T, G and dT are variable size matrix. But this would be wrong because the size of these arrays actually does not change during the simulation!
Another point: If I do so and specify that they are variable size matrix, I get the following error messages:
Simulink cannot propagate the dimension mode from the output port 1 of 'Boxes/Bus Creator' to the input port 1 of 'Boxes/Memory'. One of the multiplexed signals at the output of the source has variable-size mode. This multiplexed signal has to be demultiplexed before Simulink can propagate it to the destination.
and
Output 'dT' (#166) has variable size but the upper bound is not specified; explicit upper bound must be provided.
So I cannot use the Memory block with a variable size matrix and I have to explicit the upper bound of each variable size parameter.
Maybe I should explicit that these arrays are fixed size matrix, but I don’t know how.
Many thanks in advance for your help!
Floriane
  3 个评论
Kaustubha Govind
Kaustubha Govind 2014-7-10
Shruthi: You can configure that on the Ports and Data Manager in the right pane after selecting the corresponding parameter on the left pane.
Shruthi Kubatur
Shruthi Kubatur 2014-7-21
Hi,
Thank you for your answer. I had a follow-up question. Let's say I have a struct that is being called (as a parameter) by an S-function block. How do I update (or change) certain fields of the struct inside the simulink block and have a copy of the struct written to matlab workspace?

请先登录,再进行评论。

回答(3 个)

Mike Hosea
Mike Hosea 2012-1-18
All signal inputs to an Embedded MATLAB Function block are inherently variable, even if they are emitted from a constant block. Why are you passing m in? I think you have a couple of options here. Either make it a non-tunable parameter or remove the input altogether and make
m = size(P,2);
the first line of code of the function. Or perhaps
G=zeros(size(P));
dT=zeros(size(T));
would be correct. Anyway, the point is that to the extent possible, you should infer sizes from sizes, or make it a non-tunable parameter. -- Mike

jabbar kamali
jabbar kamali 2013-8-10
hello Mike Hosea! are u sure u have understood the questions? what u have said is compelletly unrelated to the issue!!!!!!!!!!!!
  1 个评论
Kaustubha Govind
Kaustubha Govind 2013-8-12
jabbar: Please post follow-up questions as a comment on the original question, and not as a new answer. Mike's answer does indeed address your question precisely, but perhaps you need some more context to understand it.
First of all - what is the variable 'm' in your block? Is it an input or a parameter? If it is an input, you're saying that the size of the outputs G and dT vary with the input signal - since they are defined as zeros(1,m). Perhaps you meant for 'm' to be a non-tunable parameter? Marking it as a non-tunable parameter means that it's value does not change during simulation, so none of the signals will be variable-size.

请先登录,再进行评论。


Abdulrahman Talib
Abdulrahman Talib 2017-11-19
Please if any one can help me to solve this question ,I would to represent this equation in matlab function in simulink
ò`=J'[-B(ò, o)ò - gG(ò, o) +Eu] and to know this parameter is matrix

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by