Main Content

cast

Cast variable to different data type

Description

example

b = cast(a,'like',p)convertsato the samenumerictype, complexity (real or complex), andfimathasp. Ifaandpare both real, thenbis also real. Otherwise,bis complex.

Examples

collapse all

Define a scalar 8–bit integer.

a = int8(5);

Create a signedfiobject with word length of24and fraction length of12.

p = fi([],1,24,12);

Convertato fixed point withnumerictype, complexity (real or complex), andfimathof the specifiedfiobject,p.

b = cast(a,'like', p)
b = 5 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12

Define a 2-by-3 matrix of ones.

A = ones(2,3);

Create a signedfiobject with word length of16and fraction length of8.

p = fi([],1,16,8);

ConvertAto the same data type and complexity (real or complex) asp.

B =cast(A,'like',p)
B =1 1 1 1 1 1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 8

Write a MATLAB®algorithm that you can run with different data types without changing the algorithm itself. To reuse the algorithm, define the data types separately from the algorithm.

This approach allows you to define a baseline by running the algorithm with floating-point data types. You can then test the algorithm with different fixed-point data types and compare the fixed-point behavior to the baseline without making any modifications to the original MATLAB code.

Write a MATLAB function,my_filter, that takes an input parameter,T, which is a structure that defines the data types of the coefficients and the input and output data.

function[y,z] = my_filter(b,a,x,z,T)% Cast the coefficients to the coefficient typeb = cast(b,'like',T.coeffs); a = cast(a,'like',T.coeffs);% Create the output using zeros with the data typey = zeros(size(x),'like',T.data);fori = 1:length(x) y(i) = b(1)*x(i) + z(1); z(1) = b(2)*x(i) + z(2) - a(2) * y(i); z(2) = b(3)*x(i) - a(3) * y(i);endend

Write a MATLAB function,zeros_ones_cast_example,电话my_filterwith a floating-point step input and a fixed-point step input, and then compares the results.

functionzeros_ones_cast_example% Define coefficients for a filter with specification% [b,a] = butter(2,0.25)b = [0.097631072937818 0.195262145875635 0.097631072937818]; a = [1.000000000000000 -0.942809041582063 0.333333333333333];% Define floating-point typesT_float.coeffs = double([]); T_float.data = double([]);% Create a step input using ones with the% floating-point data typet = 0:20; x_float = ones(size(t),'like',T_float.data);% Initialize the states using zeros with the% floating-point data typez_float = zeros(1,2,'like',T_float.data);R %un the floating-point algorithmy_float = my_filter(b,a,x_float,z_float,T_float);% Define fixed-point typesT_fixed.coeffs = fi([],true,8,6); T_fixed.data = fi([],true,8,6);% Create a step input using ones with the% fixed-point data typex_fixed = ones(size(t),'like',T_fixed.data);% Initialize the states using zeros with the% fixed-point data typez_fixed = 0(1、2、'like',T_fixed.data);R %un the fixed-point algorithmy_fixed = my_filter(b,a,x_fixed,z_fixed,T_fixed);% Compare the resultscoder.extrinsic('clf','subplot','plot','legend') clf subplot(211) plot(t,y_float,'co-',t,y_fixed,'kx-') legend('Floating-point output','Fixed-point output') title('Step response') subplot(212) plot(t,y_float - double(y_fixed),'rs-') legend('Error') figure(gcf)end

Input Arguments

collapse all

Variable, specified as afi对象或数值变量。

Complex Number Support: Yes

Prototype, specified as afi对象或数值变量。To use the prototype to specify a complex object, you must specify a value for the prototype. Otherwise, you do not need to specify a value.

Complex Number Support: Yes

Tips

Using theb = cast(a,'like',p)syntax to specify data types separately from algorithm code allows you to:

  • Reuse your algorithm code with different data types.

  • Keep your algorithm uncluttered with data type specifications and switch statements for different data types.

  • Improve readability of your algorithm code.

  • Switch between fixed-point and floating-point data types to compare baselines.

  • Switch between variations of fixed-point settings without changing the algorithm code.

Version History

Introduced in R2013a