1

I have a problem that many others had before, so I read some discussions and references before asking the following:

I have a Matlab function block in Simulink, which would like to be a modulator. It gets as input a [nx1] frame of data and should return a an [lx1] frame, where either l=n or l = n/K, for some K which divides n.

However, the (very simplyfied extract of) code

function ak = Modulator(dataFrame,dataType)

      coder.varsize('ak',length(dataFrame));

      M = 4; % this is for simplicity in this extract

      if dataType == 1 %input is a binary stream, bit mapping required
              ak = zeros(round(length(dataFrame)/log2(M)),1);
      else % input data is a stream of integer
              ak = zeros(length(dataFrame),1);
      end  

  end

Doesn't compile because

"Data 'ak' is inferred as a variable size matrix,
 while its specified type is something else."

Now, in line 2 I specified that it is a variable size matrix, and I also used an if/else constructor to initialize it.

To make the compiler happy, one may check the "Variable number of columns" checkbox for ak, in the Data and Ports Manager, but this turns out in a new error, because the blocks in cascade don't accept variable data, at least the ones I need, like digital filters.

2
  • Have you tried making use of the dims-option in coder.varsize? Commented Jun 17, 2013 at 12:51
  • Yes I did, but apparently it doesn't make any difference to put or not the coder.varsize command.. Commented Jun 17, 2013 at 15:16

2 Answers 2

1

First you should see whether you want your data to be variable-size. Based on the sample code it looks like you want sizes to be different based on dataType. dataType feels like a compile time (not run-time) variable. If this is the case you should make this a parameter in MATLAB Function block and make sure it is not tunable. You can then set the value of this parameter using set_param function. If you do that then the value of dataType is known at compile time and only one of the "if" branches is analyzed which would result in fixed-size "ak".

If that is not the case and you want to switch dataType at run-time then you must check the Variable size option in data and ports manager. Your ak is variable size data in this case. This output can only be used with other blocks which can support variable size inputs.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. My case is the first one, i.e. the size of the array is determined at compile time. So, first I had dataType as Tunable, and I've fixed this. But it's not enough indeed. Then, where am I supposed to place the set_param command? I can't put it inside the the function block, as I get "set_param" is not compatible for code generation, and the same applies if I put it in the mask above the block..
Your set_param should be outside your model code. You have to call this from MATLAB command line before running the model.
I found the true reasons of my problem. I'm sorry because the sample code I gave was too much simplified. Thanks for the attention. I accept you answer since all of what you said is correct. Only, here there is no need to use the trick of calling set_param. I can't give you a +1 because I'm new, sorry! Best wishes
1

The code I gave above didn't compile because of two issues:

  • the initialization of ak depends on the value of dataType, which is not an input, but a parameter defined in the mask above the MATLAB function block. However, it was marked as "tunable", so that the choice of one of the two branches of the if/else was made at runtime. This made ak a variable sized array at runtime, and hence the error.

  • To understand the second error, consider this more complete version of the sample code I gave, which was too simplified (I apologize for this)

    function ak = Modulator(dataFrame,modscheme,dataType)

    persistent constellation
    persistent M
    if isempty(constellation) || isempty(M)
        switch modcode
            case 1
                constellation = get_it_from_some_function;
                M = xx;
            case n
                ...
        end
    
    if dataType == 1 %input is a binary stream, bit mapping required
              ak = zeros(round(length(dataFrame)/log2(M)),1); % <- ERROR!!
    else % input data is a stream of integer
              ak = zeros(length(dataFrame),1);
    end      
    end
    

This code doesn't compile because as the compiler sees it, M may change at runtime, and hence the size of ak. Actually this would never happen, because M is assigned only once, but the compiler do not accept it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.