Forced variable type in MATLAB

I have variables of type double that I want them to be float . Is there a way to make the variable be a float , not a double, also, is there a way to do it in some global way for all functions and subfunctions with several lines at the beginning of the main function? I have many functions and they use many temporary variables and create the variables that they return. Performing all my functions and changing them will be very difficult.

My rational for this request:

I am writing a MATLAB program to simulate an algorithm that I will implement on a hardware level. I want to make sure that using 32-bit size as the size of my signals will not cause calculation errors.

+6
types matlab
source share
2 answers

Using B=single(A) , as suggested by @cbz, or defining arrays as SINGLE , for example by calling B=zeros(3,3,'single') , creates "floats" in Matlab.

It is not possible to globally turn Matlab into a floating point environment. Although most of the lower-level functions are also implemented for single (with some exceptions, for example, mentioned in the DOUBLE help), many high-level built-in functions will work only with double .

In other words, you will have to manually define your variables as single , you will have to periodically check that the variables are not quietly converted to double , and in the end your code may not work if it needs a function that is not yet implemented for single .

+6
source share

MATLAB is equivalent to "float" - "single". You can convert using

 B = single(A). 

However, your suggestion that this might be 32-bit might need to be reviewed. It is not so easy.

+4
source share

All Articles