I am working with a MATLAB function that uses numbers in the binary base. To do so it uses the function dec2bin to transform an integer into a char array containing the binary information. The issue is that I plan to use HDL Coder to generate a HDL version of the function. One step of the process is to convert the variables to fixed point. This can be done automatically when the data is a scalar, so is there any way to manage binary numbers without using vectors?
dec2bin is just for display purposes. Numbers are always stored in the computer using binary representation. You can use the functions
bitand,
bitor,
bitxor,
bitcmp,
bitshift,
bitget, and
bitset
to do bit-wise manipulation of integer numbers:
>> a = uint32(7);
>> b = uint32(12);
>> bitand(a, b)
ans =
uint32
4
(Click on the function names above for the documentation. You can also do help bitand in MATLAB to read a shorter version of the documentation or doc bitand to read the full documentation.)
Related
Here is the code:
x = rand(5)*100;
save('pqfile.txt','x','-ascii','-tabs')
The above works, but:
x = rand(5)*100;
x = uint8(x);
save('pqfile.txt','x','-ascii','-tabs')
says:
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'x' not written to file.
Does anyone know why this happens? How come I can't save the data when it is uint8. I have to read data into a VHDL testbench so was experimenting. I guess the only option is to save my 8 bit unsigned integer values in 2d array using printf then read into the test bench.
ASCII option
The save method is somewhat restrictive in what it can support, and then it uses floating point notation to represent your numbers which bloats your file when dealing with a limited range of numbers like you are (i.e. uint8, 0 to 255).
Check out dlmwrite as an alternative (documentation here).
It takes the filename to write/save to, the variable to store, and some additional parameters, like the delimiter you want to separate your values with.
For your example, it looks like this
x = rand(5)*100;
x = uint8(x);
dlmwrite('pqfile.txt',x,'\t');
Binary option
If you are looking to stored your uint8 data as single bytes then you probably want go with a custom binary file instead instead of ASCII. (Yes, you can convert uint8 to single ASCII characters but you run into issues with these values being interpreted with your delimiters; newlines or tabs.)
fid=fopen('pqfile.dat','wb');
if(fid>2)
fwrite(fid,size(x),'*uint8'); % Note: change data type here you are dealing with more than 255 rows or columns
fwrite(fid,x','*uint8'); % Transpose x (with x') so it is stored in row order.
fclose(fid);
else
fprintf(1,'Could not open the file for writing.\n');
end
I'm not sure what type of parser you are using for your VHDL, but this will pack your data into a file with a short header of the expected dimensions followed by one long row of your serialized data.
To read it back in with MATLAB, you can do this:
fid = fopen('pqfile.dat','rb');
szX = fread(fid,2,'uint8');
x = fread(fid,szX,'*uint8')'; % transpose back if you are dealing with matlab.
fclose(fid);
The transpose operations are necessary for MATLAB because it reads data column-wise, whereas most other languages (in my experience) read row-wise.
Now I'm researching on Image processing about interpolation. So I invented my new interpolation algorithms about image.
And I have to check SSIM value about my images that are interpolated by my interpolation algorithms.
Now I'm using ICY tool (On windows, GUI environment) but this tool is too hard to check large amount of images because of GUI interface.
So I want to check image SSIM value using matlab ssim function.
but I'm not a expert in matlab language, so I'm in trouble in dealing with import various name to ssim function.
This is what I want to do:
I want to checks ssim value with a large amount of images.
So I'm going to use sprintf function to make a valuable image name.
The third I want to import this input image name to ssim function.
This is the code I used.
for n= 1 : 10
str = sprintf('./x2/cutted/x2_fn%d_4p_3p_cutted.bmp',n);
str_ori = sprintf('ori_%d.bmp',n);
img_cutted=imread(str);
img_ori=(str_ori);
[ssimval,ssimmap]=ssim(str,str_ori);
end
When I use this code, the error is invoked.
Error: The str must be the types of uint8, uint16, int16, single, double.
but you used char value.
After I got this message, write "uint8 str;" but still the message is invoked.
ssim gets 2 images as input, not 2 strings. replace your code by [ssimval,ssimmap]=ssim(img,img_ori);
for i=1:30
Name(i,1)=sprintf('String_%i',i);
end
I'm just confused what is not working here, this script seems very straightforward, wnat to build a list of strings with numbering from 1 to 30. getting error
Subscripted assignment dimension mismatch.
Matlab do not really have strings, they have char arrays. As in almost any programming language Matlab cannot define a variable without knowing how much memory to allocate. The java solution would look like this:
String str[] = {"I","am","a","string"};
Similar to the c++ solution:
std::string str[] = {"I","am","another","string"};
The c solution looks different, but is generally the same solution as in c++:
const char* str[] = {"I","am","a","c-type","string"};
However, despite the appearances these are all fundamentally the same in the sense to that they all knows how much data to allocate even though they would not be initiated. In particular you can for example write:
String str[3];
// Initialize element with an any length string.
The reason is that the memory stored in each element is stored by its reference in java and by a pointer in c and c++. So depending on operating system, each element is either 4 (32-bit) or 8 (64-bit) bytes.
However, in Matlab matrices data is stored by value. This makes it impossible to store a N char arrays in a 1xN or Nx1 matrix. Each element in the matrix is only allowed to be of the same size as a char and be of type char. This means that if you work with strings you need to use the data structure cell (as also suggested by Benoit_11) which stores a reference to any Matlab object in each element.
k = 1:30;
Name = cell(length(k),1);
for i=k
Name{i,1}=sprintf('String_%i',i);
end
Hope that the explanation makes sense to you. I assumed that according to your attempt you have at least some programming experience from at least one other language than matlab.
With the fixed-point license it is easy to create arbitrary fixed-point data types in Matlab ie a signed 32 bit number with 16 fractional bits:
custom_sfix = fixdt(1,32,16)
custom_sfix =
Simulink.NumericType
DataTypeMode: 'Fixed-point: binary point scaling'
Signedness: 'Signed'
WordLength: 32
FractionLength: 16
IsAlias: false
DataScope: 'Auto'
HeaderFile: ''
Description: ''
How do I create a value of this type in matlab?
For built in types it is just a = int8(5); or a = uint32(45);
I have also tried types cast, but this only seems to accept the built in datatypes.
>> Y = typecast(12.5, custom_sfix )
Error using typecast
The second input argument must be a character array.
>> Y = typecast(12.5, 'fixdt(1,32,16)' )
Error using typecast
Unsupported class.
I believe fixdt is for creating fixed-point data type signals in Simulink, rather than MATLAB. You would then define your signal to be of that data type as shown in http://www.mathworks.co.uk/help/simulink/ug/working-with-data-types.html#f14-90565.
If you want to create fixed-point objects in MATLAB, you probably want to use fi instead. There are casting examples in the doc too.
Edit: Problem was that even specifying the precision of the inputs matlab still converts them to doubles unless you specify otherwise. My mistake.
Reading a simple 64 bit integer into matlab appears to be giving a different value than if I do the conversion in python or windows calculator.
I have a small file, 8 bytes long whose contents are
0x99, 0x1e, 0x6b, 0x40, 0x27, 0xe3, 0x01, 0x56
I use the following in matlab:
fid = fopen('test.data')
input = fread(fid, 1, 'int64')
I get
input = 6197484319962505200
However, using either python or the windows calculator I get a different decimal representation for 0x5601e327406b1e99. Both predict I should get
input = 6197484319962504857 (which is different by 343). Its obviously not an endianness issue as then it would be WAY off.
I originally was led to test this because reading doubles from a large binary file was giving odd results. I then tried just reading them in as integers and comparing by hand.
My question is, am I doing something wrong, is there something I am overlooking, or is matlab making an error here? I am using win64 matlab R2010a.
The problem seems to be that fread is actually reading it in as a double:
>> class(input)
ans =
double
Because the number is so large, that's presumably the closest double value.
It works for me if I manually specify that the matlab variable should be int64, in addition to specifying the type in the source file (see the documentation for fread):
>> input = fread(fid, 1, '*int64')
input =
6197484319962504857