Unable to resolve the name 'half.typecast'
8 views (last 30 days)
Show older comments
Hi guys,
I'm trying to understand the following line of code, where data is a 4D array of type uint16.
data_single = single(half.typecast(data));
I tried to digest the line by doing the following
B=half.typecast(data);
,but I'm getting the following error
Unable to resolve the name 'half.typecast'.
My problem is not with the single part, but with half.typecast() part. Any feedback will be appreciated.
0 Comments
Accepted Answer
James Tursa
on 1 May 2024
Edited: James Tursa
on 2 May 2024
half is an opaque data type in MATLAB, not a regular numeric type like double and single. I tried to get TMW to change this to a regular numeric type many years ago, but alas they have kept it as opaque and many problems followed ... this is one of them. The typical typecast() function will not work with the half type because of this. The member function half.typecast() must be used instead. That function is intended to convert the half precision bit patterns present in the uint16 variable into a MATLAB half precision data type variable. E.g.,
>> k = uint16(1:4)
k =
1×4 uint16 row vector
1 2 3 4
>> typecast(k,'half') % regular typecast() function doesn't work!
Error using typecast
Unsupported data type for conversion.
>> half.typecast(k) % convert the half bit patterns into half precision data type
ans =
1×4 half row vector
1.0e-06 *
0.0596 0.1192 0.1788 0.2384
I seem to recall that when half() was first introduced that it was missing the typecast() functionality and this method was added in later versions. What version of MATLAB are you running with?
As an alternative, consider this FEX submission that can turn those half bit patterns present in your uint16 array into a single array (you will need to install a C compiler):
E.g., compiled and run on my machine (R2020a) to show it gets the same numeric result as MATLAB half.typecast() function:
>> mex halfprecision.c -R2018a
Building with 'MinGW64 Compiler (C)'.
MEX completed successfully.
>> halfprecision(k,'single') % convert the half bit patterns into single precision data type
ans =
1×4 single row vector
1.0e-06 *
0.0596 0.1192 0.1788 0.2384
Unfortunately, the halfprecision mex routine cannot convert the uint16 directly into a half variable because the data area of opaque variables are hidden and not directly accessable to a mex routine. (Another problem ...)
*** UPDATE ***
I just checked and the "unable to resolve" issue is because half() is only available in the following toolboxes:
MATLAB Coder
Fixed-Point Designer
GPU Coder
So you must use the mex routine instead to convert these to single precision variables. You can't convert them to half() unless you have one of those toolboxes.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!