'Equivalent of reading Python files in bytes 'rb' in MATLAB?

I read some files in Python as follows:

with open("C:\Users\MyUser\Downloads\testPicture.jpg", 'rb') as FID:
    fileInBytes = FID.read()

This returns a bytes string in Python.

Running type(fileInBytes) returns <class 'bytes'>.

I need to call a Python function from MATLAB. The Python function expects the file instance as a bytes string, as described above. So, I would need to do the following in MATLAB:

  • Read in file from MATLAB in format, which will map, when calling a Python function from MATLAB, as a bytes string.

From this document, I believe, uint8 in MATLAB converts to bytes in Python.

How can I achieve this? I'm guessing I just need to read a file in uint8 format in MATLAB and pass that into Python



Solution 1:[1]

According to the fopen manual page, files are opened in binary mode unless t is in the permission string.

According to fread manual page, precision, there is reading into uint8:

fread(fid, '*uint8');

Presto.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1