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

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

Can you use just the Constant block, specify its value as zero(x), where "x" is a positive integer value in the base workspace?
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

Ok, we’re coming closer. With your help I was able to find out that if I give the Matlab Function block the dimension information directly via an integer like above, Simulink is not able to infer the output dimension and I’ll have to handle the variable-size problem. If I instead pass a sample matrix [like via zeros (n, m)] to the block and let the block extract the dimensions via zeros (size (u)) internally, then Simulink – with this extra step – is able to infer the size and I can avoid the variable-size problem.
Do you have an idea why this is the case? Both variants should end up to be exactly the same, if you ask me. I don’t really get with simulink can infer the size if I make it more complex , but isn’t able to infer if the information is directly at hand.
// x and y change every timestep, but the dimensions are fixed for the whole duration of one simulation (but I have to conduct several simulations with different dimensions each).
My guess is that MATLAB Function block executes at every simulation step and regards every input and output changing at every simulation step. So if you have the size as an input, it needs to prepare for variable size input/output.
If you provide a dummy matrix (with the desired size) as an input, it regards the value of the matrix changing but the size of the matrix fixed.
If the example works for you, you could declare the size (n and m) as the parameters of the MATLAB Function block. It has the same effect as you providing x=zero(n,m) as an input and you have to run size(x) to get the value of n and m. You could pass the value of n and m directly to the MATLAB function block through parameters.
When I put a number into a Constant block, Matlab should be able to infer that this is a constant value, the same way, as it infers it from the dimensions of a signal. Would be good if you can add this to your internal bug tracker, in my seraches I saw that I was not the only one running into this issue. The precompiler should be able to determine that such a value will never change over time and handle it as such.
Passing the variables as parameters is something I already tried, but also here Matlab is not able to infer that this value is constant and again leads to the variable-size problem. Have you tried it? I went to the Model Explorer and set the input signal’s value to variable dims which I added to the workspace by dims = 5.
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.
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.

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

카테고리

제품

릴리스

R2020a

질문:

2021년 4월 12일

편집:

2021년 4월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by