Cannot use input value with 'numerictype' in MATLAB Fcn

1 view (last 30 days)
The code below works fine
function y = fcn(u)
word_length = u.WordLength;
bin_pt = 5;
T = numerictype(1,word_length, bin_pt);
y = reinterpretcast(u,T);
But I want user-defined binary point and when I take it as an input (from a mask variable or a constant) it doesn't
function y = fcn(u, bin_pt)
word_length = u.WordLength;
% bin_pt = 5;
T = numerictype(1,word_length, bin_pt);
y = reinterpretcast(u,T);
This gives the following error
matlab_error.PNG

Answers (1)

Walter Roberson
Walter Roberson on 15 May 2019
Every signal in Simulink must be of specific type, and fixed point values of different length are considered different data types. The consistency checking is done at the time the model is to be built, but the building process compiles in specific internal routines and lengths.
Options include:
  • Permitting your user to choose only one of a limited number of widths and build distinct subsystems for every permitted width that hold all of the blocks that need to know about that width, with the block boundaries at the point where the computations no longer rely upon the width;
  • Variant controls https://www.mathworks.com/help/simulink/ug/switching-between-variants.html
  • there might be options about busses that carry around the length and an array of bytes and then at each point you switch on the length to choose the appropriate instruction and push the results back into a bus again.
  3 Comments
Walter Roberson
Walter Roberson on 15 May 2019
function y = fcn(u)
switch u.WordLength
case 1
T = numerictype(1, 1, 5);
case 2
T = numerictype(1, 2, 5);
case 3
T = numerictype(1, 3, 5);
case 4
T = numerictype(1, 4, 5);
case list off all of the other user possibilities individually
end
y = reinterpretcast(u,T);
M. Saad
M. Saad on 20 May 2019
I think I couldn't make myself clear, my case was otherwise. However, I have solved it as following, might it help someone.
I used Data Type Propagation block with a Data Type Conversion block. Input is at Ref 1 to keep the number of bits same and Ref 2 is provided by the constant block whose signedness and binary point I can modify according to the user input. In Data Type Propagation block one can parameterize which things to change from which references.

Sign in to comment.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!