I am looking to compare arrays of type uint8 in Matlab and wondering if there is a function to compare them. So, I have something like:
par_id = uint8([0x00 0x00 0x4d 0x4f 0x54 0x50 0x41 0x52])
fileID = fopen(file);
# Here I read the first 8 bytes from a file
magic = uint8(fread(fileID, 8, 'uint8'));
I thought I could do a strcmp or something like that but that fails:
strcmp(uint8(magic'), par_id) // returns 0
I can do a for loop and compare them element by element but is there a built-in function that I can use?
Use isequal. It will test the size and contents of what you want to compare, and ignores the type.
Related
I would like to construct a hash table in Matlab, the keys of which are matrices of different sizes, and the values of which are also matrices. The containers.Map class only allows strings as keys. I can certainly just use a cell for the keys, a cell for the value and match the indices of the two cells. Is there a better way to construct the hash table and the associated hash function?
I just played around with containers.Map a little, it seems that you can use char arrays of any length as keys.
>> a = containers.Map;
>> a(repmat('bla',50,500)) = 1;
>> a(repmat('bla',50,500))
ans =
1
You can also convert any numeric array into a char array as follows:
>> x = randn(4)
x =
-0.7371 -0.0799 0.1129 -1.1667
-1.7499 0.8985 0.4400 -1.8543
0.9105 0.1837 0.1017 -1.1407
0.8671 0.2908 2.7873 -1.0933
>> s = char(typecast(x(:),'uint8')')
s =
''uÔ_þ翼qÿû¿/å\¬"í?éúè#¿ë?.YðjÛs´¿Ó¶Ó·PÀì?+Ç? Õ9NÒ?Üéñé¼?
°À9-(Ü?ç¥ìƺ?NsivL#V*aó¨ªò¿{Ò5«ý¿Q8ß:#ò¿í=µU~ñ¿'
Or using the full 16-bit Unicode values allowed by char:
>> s = char(typecast(x(:),'uint16')')
s =
'疺㓦쁁뿛쓆遫뿅䅀庲뿋ꁰ頳劜㿡礋쮼㿘旈帡㿨ﮢ电玼㿼譍醪㿳랝趚蠷뿴瞶ꆲ쀂伴愹?㿬ꑨ廆뿽㼝ὧ㾱?ﺳ⩝㾢棑罓턽䀁ᕾ統렆뾱'
So putting these together, it is possible to use any array (properly converted to a char array) as key into a hash table:
>> a(s) = 5;
>> a(s)
ans =
5
And, given the numeric array cast to char, it is possible to cast it back to numeric array as well (though the shape of the array will get lost):
x = randn(1,20);
s = char(typecast(x,'uint8'));
y = typecast(uint8(s),'double');
assert(isequal(x,y)) % does not throw an error
There is another alternative. It is possible to use keys of type different from a string with containers.Map, as stated in the documentation. Keys can be either char arrays, or numeric scalars; they cannot be numeric arrays:
>> a = containers.Map('KeyType','double','ValueType','double');
>> a(5) = 10;
>> a([5,3]) = 5;
Error using containers.Map/subsasgn
Specified key type does not match the type expected for this container.
Thus, you could compute a hash value (as a floating-point double value or 64-bit integer value) from your arrays. How to best do this I don't know, maybe the dot product with a set of random values? At this related question there are some suggestions. There are also some functions on the MATLAB File Exchange that would be helpful (e.g. here and here).
I read from the serial port 4 bytes and I want to create a Matlab
function to convert them into a float number
for example: if I read A=[65 240 0 0] I must have 30 according to
IEEE754 standard.
- I have used Simulink block "byte unpack" but i have problems. in fact
I should read over 18 parameters. each parameters is 4 bytes
array.then I should use 18 byte unpack.
By default, Matlab will use double precision to store any new value which doesn't have a type specified. If you know you are reading byte, then the best is to collect them directly as uint8 (the unsigned byte type of Matlab) if you can (in your call to fread or equivalent).
If you cannot collect them directly as uint8 then cast them as such then use the typecast function.
A = [65 240 0 0] ; %// Collected bytes
A = uint8(A) ; %// cast them to "uint8" if they are not already
Afloat = typecast( A , 'single') ; %// cast the 4 bytes as a 32 bit float
wait a minute:
Afloat =
8.6186862e-41
oops, it seems the byte ordering used by your collection mechanism is the opposite as the one used by Matlab. No problem, you can just change the endianness by "flipping" the byte array.
So instead, use:
>> Afloat = typecast( fliplr(A) , 'single')
Afloat =
30
success :)
You can also look at the function swapbytes to manage the endianess.
To avoid reinventing the wheel, simply use
A = fread(obj,size,'precision')
as described in documentation
For example,
[A,count] = fread(obj, 18, 'float32');
should read 18 4 byte floats.
The C-Code is stored in a DLL. I can load the DLL in MATLAB with the loadlibrary function. I am having trouble passing the wchar_t*[] parameter to the function. I do not know how to create this data type in MATLAB. Does anyone know how to create this type to pass to the calllib function?
MATLAB Code:
loadlibrary('test.dll', 'test.h');
str = '0';
ptr = libpoiner('voidPtrPtr', [int8(str) 0])
calllib('test.dll', 'testFunction', ptr) %this parameter does not match the wchar*[] type
outVal = ptr.Value
C-Code:
void testFunction(wchar_t* str[])
{
str[0] = L"test";
}
Output:
MATLAB allows the function to complete. The outVal variable is filled with garbage values.
If you are able to modify the C header files, you may try the following:
Adjust the header file to convert all wchar_t * to unsigned short *.
On the MATLAB side, the corresponding type would then be a uint16 array.
You could then typecast the uint16 array to char.
I figured it out. I changed the MATLAB code to the following:
loadlibrary('test.dll', 'test.h');
str = '0';
ptr = libpoiner('voidPtrPtr', [uint16(str) 0])
calllib('test.dll', 'testFunction', ptr) %this parameter does not match the wchar*[] type
outVal = ptr.Value
expectedOutput = char(outVal); %convert to ASCII
It outputs the values in decimal which confused me. When I converted them to ASCII, everything made sense.
Hi I have the following object in matlab:
class(data{1}) =
char
which is stored in
class(data) =
cell
however I am trying to call:
[estt,este] = hmmtrain(data{1},e,t);
and get an error:
??? Error using ==> hmmtrain at 209
Seqs must be cell array or numerical array.
Is there a way to make each element of data compatible with the hmmtrain function?
thanks very much
For your sequence, data{1} is a char array, so convert each character into it's ASCII code via double:
[estt,este] = hmmtrain(double(data{1}),e,t);
If you want to feed hmmtrain multiple sequences with the option of using a cell array for the first input argument (as it looks like you many want to with data being a cell) try the following,
dataNumCell = cellfun(#double,data,'UniformOutput',false);
[estt,este] = hmmtrain(dataNumCell,e,t);
EDIT: Updated multiple sequence option where hmmtrain had extra double.
I have a char array representing a binary number for example
bit <1x8 char> '00110001'
I want to replace the last char with a logical value. The following error is triggered: Conversion to char from logical is not possible.
This is my code:
bit(end:end) = hiddenImg(i,j);
I checked that hiddenImg(i,j) is in fact a logical value.
This may not be optimal but should do what you want (convert the logical to a char):
>> bit = '10010100'
bit =
10010100
>> bit(end)=num2str(true)
bit =
10010101