Convert cell array containing hex and scientific notation into hex

I have 4-digit hex data coming in that sometimes displays all numeric hex entries as scientific notation, for exaple:
Sample = {'4a3';'8.00E+1';'3f00';'503'}
I need the data to be all hex. Does anyone know of an efficient way to to convert scientific notation stored as a string in a cell array to hex? Thank you in advance.

 採用された回答

Stephen23
Stephen23 2022 年 4 月 14 日
編集済み: Stephen23 2022 年 4 月 14 日
Method one: CELLFUN:
C = {'4a3';'8.00E+1';'3f00';'503'}
C = 4×1 cell array
{'4a3' } {'8.00E+1'} {'3f00' } {'503' }
X = contains(C,["+","-"]);
F = @(s)sprintf('%x',sscanf(s,'%f'));
C(X) = cellfun(F,C(X),'uni',0)
C = 4×1 cell array
{'4a3' } {'50' } {'3f00'} {'503' }
Method two: COMPOSE:
C = {'4a3';'8.00E+1';'3f00';'503'}
C = 4×1 cell array
{'4a3' } {'8.00E+1'} {'3f00' } {'503' }
X = contains(C,["+","-"]);
C(X) = compose('%x',sscanf(sprintf(' %s',C{X}),'%f'))
C = 4×1 cell array
{'4a3' } {'50' } {'3f00'} {'503' }

1 件のコメント

Gregory Jelic
Gregory Jelic 2022 年 4 月 14 日
Method one works beautifully, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by