Help converting CAN Payload in String format to engineering unit
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Harpreet Singh
am 5 Jul. 2024
Kommentiert: Harpreet Singh
am 16 Jul. 2024
Hello,
I have the following valiable in Matlab which holds the CAN data from a recorder in the following format:
Hexadecimal

The output of Row 1047 Should be -1951 normally and -1.951 after factoring it by 0.001. This conversion is done using this website.This is basically a force value of a Dynamometer. How can I do this conversion in Matlab? Any known function?
The DBC is as follows:

2 Kommentare
Stephen23
am 16 Jul. 2024
T = "61,F8,FF,FF";
N = double(typecast(uint8(sscanf(T,'%x,')),'int32'))*0.001
Akzeptierte Antwort
Arjun
am 15 Jul. 2024
As per my understanding you have a table of values and you are specifically interested in converting the payload from a comma separated hexadecimal string to a number in engineering unit and then scale it using the scaling factor of 0.001. One way to do so is as follows:
- First parse the payload string and remove the comma using the split function to convert into 4x1 string array.
- Convert the string so obtained to decimal from hexadecimal using hex2dec function.
- Convert the data to bytes using the uint8 function.
- Use the typecast function to convert the bytes data to signed 32-bit Integer.
- Finally apply the scaling factor to get the desired result.
You can refer to the following code that corresponds to the above approach:
%String to be converted
payload_str = "61,F8,FF,FF";
%Conversion to bytes after removing comma and converting to decimal
payload_bytes = uint8(hex2dec(split(payload_str, ',')));
%Interpret as a 32-bit signed integer
raw_value = typecast(payload_bytes, 'int32');
scaling_factor = 0.001;
%apply the scaling factor
engineering_value = double(raw_value) * scaling_factor;
disp(engineering_value);
The code produced an output of –1.951 for the given inputs.
Please refer to the following documentation links for more information on the split, uint8, hex2dec and typecast functions respectively:
- https://www.mathworks.com/help/matlab/ref/split.html#d126e1564444
- https://www.mathworks.com/help/matlab/ref/uint8.html
- https://www.mathworks.com/help/matlab/ref/hex2dec.html
- https://www.mathworks.com/help/matlab/ref/typecast.html
I hope this helps!
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
