How to Change the data type of MATLAB user defined function block in SIMULINK to a fixed point 8 bit or 16 bit

5 views (last 30 days)
Hello all,
I am working in the HDL coder tool in Simulink.
In simulink I have written a matlab code using MATLAB user defined function and simulated it successfully. But the out from the MATLAB function is by default is double, is there any method to change the data type to fixdt point 16 bit. This will be helpful for me while converting the model into HDL code.
Thanking you,
Roy

Answers (2)

Tim McBrayer
Tim McBrayer on 27 Aug 2014
There are many ways to do this. The most direct will be to define the data type of the MATLAB Function block output using the Model Explorer. It will explicitly allow you to set any desired data type on any port of any block.
A less direct method is to use Simulink's type propagation. The answer above suggests a Data Type Conversion block, placed on the output. This won;t quite do it. You would need the receiving block's input to be specified to your desired data type.
This can also be accomplished inside your MATLAB code, by explicitly setting the type of the data assigned to your function output in the assignment. For example, for the final output assignment, instead of writing:
y = my_computed_data;
Write this instead:
y = fi(my_computed_data, 1, 16, 4);
This specifies the output to be a signed 16 bit value with 4 bits to the right of the binary point. If this is the first (or only) assignment of output y, it will define the data type of y. With no explicit or implicit definition the data type defaults to double.
  2 Comments
Anish Navalgund
Anish Navalgund on 15 Apr 2018
Sir, I have multiple variables in my user-defined matlab function used in simulink. Now, if I'm using "fi" function, it is throwing multiple errors. Can I know where to declare "fi" function? Before function definition or in function body? Please help.
Bharath Venkataraman
Bharath Venkataraman on 1 Mar 2023
Edited: Bharath Venkataraman on 1 Mar 2023
fi is used within the function body. Here is the link to the documentation for fi.
If you are defining a variable var to be a fixed-point data type, you can do the following:
var = fi(0, 1, 16, 14); % signed, word length = 16 bits, fraction length = 14 bits.
Here is an example that shows design patterns for use of the MATLAB function block for HDL code generation.

Sign in to comment.


stalin
stalin on 27 Aug 2014

Community Treasure Hunt

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

Start Hunting!