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.

 Accepted Answer

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' }

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!