Issue with using Serialport to take in hex values and plot decimal

조회 수: 5 (최근 30일)
Maria Butt
Maria Butt 2021년 4월 15일
답변: Shadaab Siddiqie 2021년 4월 29일
Hello!
I am trying to plot values from proteus onto matlab, and my proteus values are shown in hexadecimal. I used hex2dec to help me with converting my signal values but my issue is that it only is looking at the positive values. Is there any way to also look at the negative values as well?
Thanks!

답변 (1개)

Shadaab Siddiqie
Shadaab Siddiqie 2021년 4월 29일
From my understanding you want to convert negative hex numbers to decimal, The ability to use these functions on negative numbers is not available in MATLAB.
To work around this issue, use the following code:
ndec2hex.m
function [hexstring]=ndec2hex(x,n)
% x : input decimal number
% n : number of bits to perform 2's complements
% hexstring : hex representation of two's complement of x
x=x + (x<0).*2^n;
hexstring=dec2hex(x, ceil(n/4));
nhex2dec.m
function [x]=nhex2dec(hexstring,n)
% hexstring : hex representation of two's complement of xmydec=hex2dec(hexstring);
% x : input decimal number
% n : number of bits to perform 2's complements
x = hex2dec(hexstring);
x = x - (x >= 2.^(n-1)).*2.^n;

카테고리

Help CenterFile Exchange에서 Instrument Connection and Communication에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by