Matlab reading 24-bit ascii-hex file into a 32-bit signed - matlab

I'd like to plot every 9th element from an ascii file containg a single column of 24-bit signed hex values
ex.
813457
123456
241566
etc..
The problem is, I can't get Matlab to treat values 800000-FFFFFF as negative, presumably because it's not sign extending it into 32-bits.
I thought of breaking it up into strings, then converting, but sscanf requires the '0x' to convert signed %i hex values so I'm forced to use unsigned:
C = textscan(fp, '%s') %generates 16380x1 cell (instead of normal array?!)
sscanf (C{1,1}{1,1}, '%x') %convert first ascii hex element from cell to unsigned hex
Interestingly, as a test, just doing hex2dec('FFC00000') results in a postive number, how can I force all the ascii lines in the file to be imported as an array of signed 24 or 32bit data?

To do the conversion you need, you can follow these steps:
Convert from hex string to a positive integer of double type, using herx2dec, as you've already done.
Convert that value to uint32 type.
Interpret that uint32 value as an int32 (convert data type without changing underlying data), using typecast.
Example:
>> typecast(uint32(hex2dec('FFC00000')),'int32')
ans =
-4194304

Related

How to convert uint8 to int in Matlab?

I have converted the image from RGB to grayscale and have the image-matrix in uint8. I'm trying to make some image processing with convolution which means that I want to use the values as int when I'm summing up everything. So, how can I convert from uint8 to int in Matlab?
Matlab support multiple integer formats. The main difference is the required space in memory and if the sign (+ or -) is used.
For example, uint8 means that the integer is unsigned and that it uses 8 bit to store the value. The number of used bits determines the maximal value. The uint8 can store a number between 0 and 2^8-1.
You can find a whole list of all supported integer here
If you would like to convert your uint8 into another format you can just write the desired format as a function and pass the value as parameter:
I2 = uint16(I);
In matlab you can use double type.
for example type cast with:
I2=double(I);
It is easy to understand that you can use
int8(I)
int16(I)
too.

Convert from int to binary or hex in mikroc

I got an int value from 0 to 255 and I want to convert that value to hex or binary so i can use it into an 8 bit register(PIC18F uC).
How can i do this conversion?
I tried to use IntToHex function from Conversion Library but the output of this function is a char value, and from here i got stuck.
I'm using mikroc for pic.
Where should I start?
Thanks!
This is a common problem. Many don't understand that, Decimal 15 is same as Hex F is same as Octal 17 is same as Binary 1111.
Different number systems are for Humans, for CPU, it's all Binary!
When OP says,
I got an int value from 0 to 255 and I want to convert that value to
hex or binary so i can use it into an 8 bit register(PIC18F uC).
It reflects this misunderstanding. Probably, because debugger is configured to show "decimal" values and sample code/datasheet shows Hex value for register operations.
So, when you get "int" value from 0 to 255, you can directly write that number to 8-bit register. You don't have to convert it to hex. Hex is just representation which makes Human's life easy.
What you can do is - this is good practise --
REG_VALUE = (unsigned char) int_value;

How to give ascii characters as input in simulink

I have to give ascii characters as input from simulink to stateflow and need to check whether the input matches with the existing ascii character. Can anyone help me to solve this? will be of a great help?
Example:
If I give ascii characters 'AF' as input from simulink to stateflow. It has to produce 1 as output if it matches with the existing ascii character in condition.
Simulink/Stateflow prefer numeric data. You should use an integer representation of the ASCII value (using a uint8 or uint16 data type), which will make comparison almost trivial.
Matlab does not make a clear distinction between a string with just one char and a char, and as far as I know, it is not possible to use a string type in stateflow.
Convert the input to integers then use only comparisons of integers inside the state chart.
You can use this function to convert chars to integers in Matlab:
function [ integer ] = atoi( char )
%ATOI Ascii To Integer converts char to int
%
integer = char - '0' + '0' ; %matlab seems a bit lunatic when it comes to chars
end

syntax of readmtx(fname,nrows,ncols,precision) in matlab

I want to read a matrix from file using the following syntax in MATLAB . This matrix is of double numbers .
readmtx(fname,nrows,ncols,precision)
Here all the inputs are quite familiar to me . But I want to know about precision . The precision of int is 'int16'. What is the precision of double number?
In this case, the documentation states:
Both binary and formatted data files can be read. If the file is binary, the precision argument is a format string recognized by fread. Repetition modifiers such as '40*char' are not supported. If the file is formatted, precision is a fscanf and sscanf-style format string of the form '%nX', where n is the number of characters within which the formatted data is found, and X is the conversion character such as 'g' or 'd'. Fortran-style double-precision output such as '0.0D00' can be read using a precision string such as '%nD', where n is the number of characters per element. This is an extension to the C-style format strings accepted by sscanf. Users unfamiliar with C should note that '%d' is preferred over '%i' for formatted integers. MATLAB syntax follows C in interpreting '%i' integers with leading zeros as octal. Formatted files with line endings need to provide the number of trailing bytes per row, which can be 1 for platforms with carriage returns or linefeed (Macintosh, UNIX®), or 2 for platforms with carriage returns and linefeeds (DOS).
In addition it is helpful to look at the table summary in the fread documentation:

How to fscanf a combination of float and string?

The data is like this
5.1,3.5,1.4,0.2,Iris-setosa
while I read it using this
data = fscanf(file, '%f,%f,%f,%f,%s');
and it turned out that data is an array of float rather than a combination of float and string. So how do I read this data from txt?
From the Matlab docs for fscanf:
Output Arguments A: An array. If the format includes:
Only numeric specifiers, A is numeric. ... Only character or
string specifiers (%c or %s), A is a character array. ... A
combination of numeric and character specifiers, A is numeric, of
class double. MATLAB converts each character to its numeric
equivalent. This conversion occurs even when the format explicitly
skips all numeric values (for example, a format of '%*d %s').
So your best bet is to read everything in as strings, and then convert the numeric strings to numeric values, using str2num or str2double or similar.
Alternatively, since you know there are 4 floating point values that really store a floating point value, and then the rest store the numeric ASCII values for the string, you can always split up your data and cast the part you know should be a string to char. Something like:
flt = data(1:4);
str = char(data(5:end));