How to prevent change of datatype fixdt(0,8,0) to uint8?

7 visualizaciones (últimos 30 días)
SLC
SLC el 13 de Oct. de 2017
Respondida: Igor Tselniker el 11 de Mayo de 2023
How can I prevent a numerical value of datatype fixdt(0,8,0) from converting to a uint8 through a datatype conversion block in Simulink which is set to output fixdt(0,8,0)? I have tried on both R2013b and R2017a.
Thanks in advance!

Respuestas (1)

Igor Tselniker
Igor Tselniker el 11 de Mayo de 2023
Hi,
I guess you are trying to make an assignment inside MATLAB function block?
I am asking since I never experienced any issues when trying to assign fixdt(0,8,0) signal(s) using "proper" Simulink blocks.
If that's the case, then let's assume you have a following MATLAB function inside your SImulink model:
function fcn
global cfg_rw_vref_mu_coarse
cfg_rw_vref_mu_coarse = fi(255 , 0 , 8 , 0);
And let's assume you've declared cfg_rw_vref_mu_coarse as Simulink signal object:
cfg_rw_vref_mu_coarse = Simulink.Signal;
cfg_rw_vref_mu_coarse.DataType = 'fixdt(0 , 8 , 0)';
cfg_rw_vref_mu_coarse.Dimensions = 1;
cfg_rw_vref_mu_coarse.Complexity = 'real';
Now, trying to run this model will fail with the following error:
This assignment writes a 'embedded.fi {uint8}' value into a 'uint8' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches. Function 'MATLAB Function' (#24.44.65), line 5, column 1: "cfg_rw_vref_mu_coarse" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'test/MATLAB Function'
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'test/MATLAB Function'
Component:Simulink | Category:Model error
So in order to fix this, one has to use a subscripted reference notation:
function fcn
global cfg_rw_vref_mu_coarse
cfg_rw_vref_mu_coarse(:) = fi(255 , 0 , 8 , 0);
Notice the colon inside the parentheses (i.e. (:))! Alternatively, since this a scalar signal, we could use (1) instead of (:).
More information on subscribed reference notation can be found in the sub-section named Subscripted Assignment at the following link which explains why this can reduce type propagation issues and render more control:
By the way, this only happens when trying to assign Simulink.NumericType object of 8, 16, 32 or 64 bits, which are converted into uint8, uint16, uint32 and uint64 respectively when Subscripted Assignment is not used.
I hope this helps:)

Categorías

Más información sobre Fixed Point en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by