I have a field from an ODBC connection where the data of a curve is stored in the following way:
  • range for single value is signed 16bit (-32768 to 32767)
  • representation in HEX 4 "digits" with padding left zeros (8000 to 7FFF)
  • all values are concatenated to a string in the database
  • no delimiters
  • number of elements is string length/4
Example:
  • Values (123, 456, -17000)
  • String contains: "007B01C8BD98"
Now I would like to us this data inside Matlab.
I could prepare the data and create a translation database. But I would prefere a direct approach.
All string splitting operators make use of delimiters. But this does not work here.
What is the Matlab/Database Explorer way to get access to that data ? Are there any ready to use functions ?

 채택된 답변

Guillaume
Guillaume 2017년 3월 21일

0 개 추천

Simply reshape the string into 4 columns, pass it through dec2hex and account for the fact that dec2hex assumes unsigned integer by casting from unsigned to signed:
s = '007B01C8BD98'; %demo data
typecast(uint16(hex2dec(reshape(s, 4, [])')), 'int16')

추가 답변 (2개)

Jan
Jan 2017년 3월 21일

1 개 추천

Based on Guillaume's solution:
typecast(uint16(sscanf(s, '%4x'), 'int16')
sscanf is much faster and nicer to convert hex to dec values.
ES
ES 2017년 3월 21일

0 개 추천

One suggestion would be, since the width of each value is fixed, you can do something like this..
string = '007B01C8BD98ABCDEFA';%Example data
idx = 1:4:length(string);
for iloop=1:length(idx)-1
value(iloop) = hex2dec(string(idx(iloop):idx(iloop+1)-1))
end

댓글 수: 2

You could avoid the loop entirely (see my answer), but if you're going to use a loop, you should at least predeclare value
value = zeros(1, numel(string)/4);
Clearly your code does not decode properly negative values.
Note that using string as a variable nam is not recommended in version >= R2016b, since it's now the name of a very useful class.
This answer also works but the "one line approach" by Guillaume seems more elegant. Thank you!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2017년 3월 21일

댓글:

2017년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by