Unable to resolve the name 'half.typecast'

9 visualizaciones (últimos 30 días)
Yazan
Yazan el 1 de Mayo de 2024
Editada: Yazan el 2 de Mayo de 2024
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.

Respuesta aceptada

James Tursa
James Tursa el 1 de Mayo de 2024
Editada: James Tursa el 2 de Mayo de 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.
  1 comentario
Yazan
Yazan el 2 de Mayo de 2024
Editada: Yazan el 2 de Mayo de 2024
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by