Type casting in Matlab - matlab

What is the proper way to cast data types in Matlab?
I have my data stream (instrument data) as undifferentiated elements of a uint8 array, which I want to cast into a composite data types, like for instance a 3-byte, 24-bit integer, or a 3-byte, 3 character string.
I normally do this on the fly specifying a format in fread(), but once the data is already in a uint8 array, is typecast() the proper way to go about it, or is there a nicer syntax?

As I know, there is not any data type for integer 3 or 24 bytes (see documentaion). However, about casting from uint8 to string, it can be done by num2str function (like num2str(uint8(123)). Also if you want get a 2 char string (from left) you can do it by num2str(uint8(123))(end-1:end);

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.

Why are MATLAB uint8 value and uint16 values different

I have declared a uint8 value as A = [4, 8, 16, 32]; and I casted to the value B = typecast(uint8(A), 'uint16'), but the answer is 2052 8208 I would be very thankful if anyone could help me to understand the reason behind it.
You are probably expecting for Matlab to put your uint8 values just into uint16 variables. This is not what the typecast function is doing. It will preserve the number of bytes from the input to the output. So in your example, it merges the bit representation of 4 and 8 into a uint16 number and equivalently also 16 and 32.
So the binary representation of 4 is 00000100 and of 8 is 00001000 and merged together (to a 16bit number) they give 0000100000000100, which is 2052.
MATLAB's typecast function converts the data type without changing the underlying data. In other words, it doesn't changes the underlying memory representation of the data structure, and simply treats it as a uint16 instead of uint8.
In your case, you want to preserve to original values of your data after casting. Therefore, you do want MATLAB to change the memory representation of the data structure.
There are two ways to perform this type of casting:
-using cast function
B = cast(uint8(A), 'uint16');
-using a direct call to uint16 function:
B = uint16(A);
result:
B =
4 8 16 32

Coercion between uint types

I have a problem in general when Matlab is unable to work out how to logically store values of differing uint types. For example:
tempC = {uint8(5) uint16(16)}
For me, it seems logical to be able to convert this into a matrix of type integer using cell2mat(tempC), which returns
>> cell2mat(tempC)
Error using cell2mat (line 45)
All contents of the input cell array must be of the same data type.
Of course, I understand that the truncation behaviour of integers depends on the type (e.g. uint8 forces all numbers greater than 255 to be 255), however, in this case I would say it would be safe enough to output cell2mat(tempC) with uint16 type. Does anyone have any ideas on how this can be achieved in general?
cell2mat will not work if there are cells of differing types. cell2mat merges the cells together into a matrix, but matrix elements in MATLAB must all share the same type. This is fundamental to how MATLAB works with numeric matrices. If you didn't have all of the same type, then you should use cell arrays... which is what they are for.
However, one thing I can suggest is figure out the type of all of the elements in your matrix, then iterate through each cell and cast them all to be the largest precision type. You can then use cell2mat on this intermediate result to complete the conversion. However, what I have written doesn't actually require calling cell2mat in the end. You'll see later.
Something like this:
%// Get all of the possible types in the array
types = unique(cellfun(#class, tempC, 'uni', 0));
%// Figure out the largest type
vals = cellfun(#(x) double(intmax(x)), types);
[~,ind_max] = max(vals);
%// Cast all values to this type
class_max = types{ind_max};
tempC = cellfun(#(x) cast(x, class_max), tempC);
We first determine all of the possible classes that your cell array contains. We then figure out which of the types is the largest of them all. This can be done by using intmax on each of the types. intmax tells you the largest possible integer that is available for that type, so we basically choose the type that generates the largest possible integer. Take note that I had to cast to double as the output of intmax certainly does output the maximum associated for an integer type, but the output is also cast to that type. This is required so that I can combine all of these elements into an array of the same type - double.
Once we get the type producing the maximum possible integer, we then go through the cell array and cast all of the values to this type. Take note that I used cellfun for the final call which outputs a numeric array - no need to use cell2mat here. In the last line of code, I use cast to cast all of the numbers in the cell array to this type, thus achieving "coercion".
Using your example array, this is what I get, as well as what class the final array is in:
>> tempC
tempC =
5 16
>> class(tempC)
ans =
uint16

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));

How can I convert double values into integers for indices to create a sparse matrix in MATLAB?

I am using MATLAB to load a text file that I want to make a sparse matrix out of. The columns in the text file refer to the row indices and are double type. I need them to be integers to be able to use them as indices for rows and columns. I tried using uint8, int32 and int64 to convert them to integers to use them to build a sparse matrix as so:
??? Undefined function or method 'sparse' for input
arguments of type 'int64'.
Error in ==> make_network at 5
graph =sparse(int64(listedges(:,1)),int64(listedges(:,2)),ones(size(listedges,1),1));
How can I convert the text file entries loaded as double so as to be used by the sparse function?
There is no need for any conversion, keep the indices double:
r = round(listedges);
graph = sparse(r(:, 1), r(:, 2), ones(size(listedges, 1), 1));
There are two reasons why one might want to convert to int:
The first, because you have data type restrictions.
The second, your inputs may contain fractions and are un-fit to be used as integers.
If you want to convert because of the first reason - then there's no need to: Matlab works with double type by default and often treats doubles as ints (for example, when used as indices).
However, if you want to convert to integers becuase of the second reason (numbers may be fractionals), then you should use round(), ceil() or floor() - whatever suits your purpose best.
There is another very good reason ( and really the primary one..) why one may want to convert indices of any structure (array, matrix, etc.) to int.
If you ever program in any language other than Matlab, you would be familiar with wanting to save memory space, especially with large structures. Being able to address elements in such structures with indices other than double is key.
One major issue with Matlab is the inability to more finely control the size of multidimensional structures in this way. There are sparse matrix solutions, but those are not adequate for many cases. Cell arrays will preserve the data types upon access, however the storage for every element in the cell array is extremely wasteful in terms of storage (113 bytes for a single uint8 encapsulated in a cell).