How to force whole m file calculation in class smaller than double (e.g:uint16)

2 views (last 30 days)
Hi all,
I`m implementing Matlab code in MCU(EFM32) by using Matlab Coder app and code replacement for ARM cortex M3. Since this micro controller has just 32K RAM, I have to use uint16 variable which is enough for me rather than double(Matlab default). I know by using uint16() I can change the class, but the think is that I do not want to manipulate the Matlab code and also because some functions call other functions,it is better to just force all calculation in other class just once. Does anybody has experience in this regard. Thanks.
  1 Comment
John D'Errico
John D'Errico on 3 Oct 2017
Sorry, but you can't do it. You cannot force a complete block of MATLAB code to operate in uint16, merely by setting a flag.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 3 Oct 2017
In a situation such as that, it is probably best to use the Fixed Point Toolbox. Even if you use it only with a built-in uint16 data type, you would be pushing your values out of the normal numeric classes, ensuring that they are not accidentally treated as belonging to some other data class. You will probably find that you want to configure the behavior on overflow (values that would exceed 65535) and underflow (values that would be negative): the MATLAB behaviors are not the ones typically implemented in hardware, so you will want to either conform to hardware or have MATLAB arrange to add extra instructions to maintain its saturation behavior.
Be especially careful about division: there are several different rounding behaviors to take into account, and sometimes you want different behavior for different calculations.
  2 Comments
Ehsan Ghaheri
Ehsan Ghaheri on 4 Oct 2017
Hi Walter, I`m using m file with pure matrix operation which always needs just unsigned integers(the elements just change their position in the matrix).I think in this case use of fix point toolbox cannot help. Thanks.
Walter Roberson
Walter Roberson on 4 Oct 2017
Be careful with your index operations: unless you write them carefully it is likely you will accidentally use double at some point.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!