Matlab typecast transfer to python 3.7 - matlab

I have some excerpt code for Matlab. I want to transfer to python3.7 code. But I found out some Matlab function I can not transfer to python 3.7 it, such as function typecast, and single in Matlab.
Therefore, How could I write the python code and get the same result?
Thank you
Here is the Matlab code
AA= uint32(3249047552)
DPD = typecast(AA,'single');
Print(DPD)
DPD = -21.0664 <== This is matlab result.

With numpy you can use view():
# Define your uint32 number
x = np.array(3249047552, dtype=np.uint32)
# Get the equivalent bitwise single number
x.view(np.single)
# output: -21.066406

Related

Work with binary numbers as scalars in Matlab

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

How can I import a function of Octave to MATLAB?

I am running my code in Matlab. But I also want to call a function in octave. How should I import the qp function of Octave to Matlab?
The Octave language is a superset of the Matlab language. If qp only made use of the Matlab language, then you could simply add it to your Matlab path and be done with it.
However, Octave's qp makes extensive use of Octave only language so you basically have to port the code yourself. There are no tools for this, you have to convert the code from one language to another. In addition, the actual solver is the function __qp__ which is written in C++ and uses liboctave.
Two easier alternatives than porting qp are:
save the data from your Matlab session into a file save foo.mat mydata, call Octave to do the work and save the results system ('octave --eval ''load ("foo.mat"); qp (...); save foo.mat ...;', and read the file back load foo.mat.
or the much simpler alternative, just use Octave.
Octave syntaxt is not totally compatible with MATLAB. For example the preferred syntax for defining function in Octave is like this:
function ret = f()
%do something
endfunction
but MATLAB doesn't accept that syntax and there are other differences like differences in calling native codes and ... so it is not simple to convert each statement of octave library to matlab or convert oct c++ source to mex.
A straightforward way is that you should have an installation of Octave and run octave script from it then save the results to a mat file and in MATLAB load the file . You may use system function to execute octave or run it from shell.
so lets say you have 2 files in the same directory. a.m and b.m In the script b.m if you type a as a line of code everything in a.m will happen (variable assignments function definitions computations etc...)
Additionally you can use the import statement for adding things to your import list. as seen here.

SSIM Coding Error

I have some questions. I try to follow some coding from Mathworks:
I = imread('cameraman.tif');
ssimValues = zeros(1,10);
qualityFactor = 10:10:100;
for i = 1:10
imwrite(I,'compressedImage.jpg','jpg','quality',qualityFactor(i));
ssimValues(i) = ssim(imread('compressedImage.jpg'),I);
end
I just change the image file which is a.jpg and b.jpg but I get this error from MATLAB:
Undefined function 'ssim' for input arguments of type 'uint8'
Error in SSIMTesting (line 6)
ssimValues(i) = ssim(imread('logohalal1.jpg'),i);
Why is that ? Can someone help me explain the code and the error ? Sorry because I'm new in MATLAB.
Thank you.
MATLAB release notes for the Image Processing Toolbox shows that this function was new to R2014a. If you have an older version of MATLAB, or you don't have that toolbox, you don't have it. This sort of issue can be avoided by using only examples found in the help on your local installation of MATLAB rather than the online help.
To check your version of MATLAB and installed toolboxes, type ver at the command line.
To check if a function can be found on your MATLAB path, you can use which, e.g. which ssim

Quantile function causing error in MATLAB

I am using quantile function in the simplest form:
x = [1.2,3,4,5];
y = quantile(x,0.5);
But I get the error:
Undefined function 'quantile' for input arguments of type 'double'.
I was not having this error in MATLAB R2009a but get this error in R2012a.
What could be the reason?
Please run:
license('test', 'Statistics_Toolbox')
In case this returns a 0 then you do not have an active license of the statistics toolbox.
However if you want to use it as in your example the following code would do:
y = median(x);
quantile is part of the statistics toolbox. If you do not have access to that in your new installation, you will not be able to use the function.
To check which toolboxes you have installed, type ver at the command prompt.

bi2de error in MatLab

In MatLab, "> help bi2de" provides the following example:
B = [0 0 1 1; 1 0 1 0];
D = bi2de(B)
But when I try this on my own, I get the following error:
??? Undefined function or method 'bi2de' for input arguments of type 'double'.
Is there something wrong with this function in MatLab?
I am pretty sure that the reason why this problem happened is because of the license of the toolbox, Communications system toolbook, in which this function belongs in. Write which bi2de and see what will be the result. If it returns path of the function and the comment Has no license available, then the problem is related to the license. That means, license of the toolbox is not set correctly. Mostly it happens if the toolbox is added later, i.e., after installation of the original matlab. Please check and solve the license issue, then it will work fine.
bi2de is a function in the Communications toolbox. You need to have that toolbox to use it. If you do have that toolbox, then the problem is that your B matrix is being treated as double instead of binary (I don't have the toolbox so I can't test this).
Consider using bin2dec, which turns a string representation ('1011001', eg) into a decimal number. This function is not part of a toolbox; it's available as part of the basic MATLAB package.