Unable to resolve the name 'half.typecast'

17 次查看(过去 30 天)
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.

采纳的回答

James Tursa
James Tursa 2024-5-1
编辑:James Tursa 2024-5-2
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.
  1 个评论
Yazan
Yazan 2024-5-2
编辑:Yazan 2024-5-2
Thank you James, I really appreciate the elaborate answer. Intresetingly enough I installed the MATLAB Coder toolbox and it worked after that, not sure why, but I'm sure it has to do with the class and the way that whole thing work togather. I have the 2024a release as well.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by