How can I cause integer overflows to make an exception in matlab scripts?

조회 수: 3 (최근 30일)
Jay Gowdy
Jay Gowdy 2013년 3월 18일
I am attempting to write a unit test in MATLAB scripts for a function that is used in an EML block in Simulink. In the EML block when there is an integer overflow I have it configured to throw an exception. In Matlab scripts when there is an integer overflow in the same function, it silently saturates the operation. How do I get the function when called from a Matlab script to behave the same as when called from an EML block? (Note: configuring the EML block to saturate on integer overflows is NOT an option).
I am using R2012b Matlab and Simulink.
  댓글 수: 1
Jan
Jan 2013년 3월 18일
This is one of the very rare cases where the tag "matlab code" makes any sense.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2013년 3월 18일
편집: Jan 2013년 3월 18일
You can't.
The silent saturation is a built-in behavior, whcih can be changed only by shadowing the concerned operators:
File: \@uint8\plus.m
function c = plus(a, b)
c = a + b;
t = double(a) + double(b);
if t > 255
error('Saturation!');
end
% Or:
% d = c - a;
% if d ~= b, error('Saturation!'); end
But such code needs an exhaustive testing also, because it is too easy to oversee some effects. Creating unit tests for testing unit tests based on shadowed builtin functions is not a clean way to work.
Therefore I suggest to create a new class for integer types with overflow. Implementing the operators in C would allow for a fair speed, but even then detecting overflows is not trivial.
  댓글 수: 1
Jay Gowdy
Jay Gowdy 2013년 3월 19일
At one time, many revisions ago, I remember a parameter setting that made this behavior possible (stopping scripts on integer overflows instead of saturation). It is sad it went away, because it makes developing in MATLAB with the ultimate goal of making EML blocks in Simulink much harder (because you can never know if the results you see in your otherwise EML-compliant MATLAB script are what you are going to see in your equivalent Simulink EML block).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Communications Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by