How to create constant(!) input-dependent matrices with a Matlab function block without that variable-size stuff

조회 수: 25 (최근 30일)
I need to implement a Matlab function block which creates matrices according to a given size specified by an input signal. The size value is constant(!) throughout a simulation, and I don’t want to use superfluous variable-size stuff, as I don’t want to overcomplicate my simulation and sample the block totally needlessly.
Easy example for the Matlab function: y = zeros(x) with x being the input signal of the Matlab function block and y being the output signal. That input gets connected to a Constant block set to a fixed value of say 5. Then this block should do nothing more than create a 5x5 matrix full of zeros. I am not able to do this without messing the whole simulation up with variable-sized signals and needless block sampling.
I would appreaciate it a lot, if someone can help, me. That already costed me days.
No matter what I do, I always get the error message:
Data 'y' is inferred as a variable size matrix, while its properties in the Model Explorer specify its size as inherited or fixed. Please check the 'Variable Size' check box and specify the upper bounds in the size field.
But that signals is not variable at all, it’s absolutely fixed! So I do not understand at all how Simulink cannot be able to infer the signal size from that constant value.
Block diagram
Block diagram
Matlab function block content
Error message
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2021년 4월 12일
Can you use just the Constant block, specify its value as zero(x), where "x" is a positive integer value in the base workspace?
Frank Brütting
Frank Brütting 2021년 4월 13일
편집: Frank Brütting 2021년 4월 13일
I simplified my problem to a minimal example, in reality I need to transform matrix signals of arbitrary size – which is why the formula in the end would be much more complex and I need to have an input and an output signal. The matrix signal size though is fixed throughout the whole simulation (I need to do lots of simulations, but each with a different signal size), and I want to pass the signal dimensions to the block via a constant expression.
In other programming languages constant expressions get optimized by the precompiler in advance, so I’m searching for the equivalent method here.

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

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2021년 4월 13일
편집: Fangjun Jiang 2021년 4월 13일
MATLAB Function block can deal with variable size. It is just a lot of annoynance like you suggested. You need to declare the max size of the variable.
The size of your "y" change per simulaiton but is determined at the begining of the simulation. But does the value of "y" change per simulation step? If not, you can do a "InitFcn" callback to create "y".
If "y" changes value at every simulation step, you can still avoid the variable size issue in MATLAB Function block. I just tried a simple example. Drag a default MATLAB Function block, connect its input to a Constant block whose value is rand(N), connect the output to a "Display" block.
Change the value of N to 2,3,4 everytime you run a simulation. It seems working as you desired.
  댓글 수: 5
Fangjun Jiang
Fangjun Jiang 2021년 4월 13일
Then ignore the "Parameter" suggestion. I didn't try it myself and I though it could work.
The question left is how MATLAB Function block determines variable size input/output, which I don't have an ansser.
Frank Brütting
Frank Brütting 2021년 4월 13일
편집: Frank Brütting 2021년 4월 13일
When I turn on color-coding, I even can see that Simulink knows that the dimensions specified in a Constant block are of constant value, due to it being colored pink! Yet Simulink is bugging me about not being able to infer the size of that constant value. I don’t get this, there seems to be an obvious minor problem in Simulink, it totally should be able to infer the dimension when it’s based off a constant value.
If you can get a developer looking at this, I think this should be relatively easy to fix. Because at some point, Simulink seems to just lose the fact that the value is constant and therefore starts to come up with variable-size.

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

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by