Primary decoding of raw images
Work with raw images begins with their converting to some readable format without changing any brightness values. Such converting can be made with dcraw by Dave Coffin (.exe file can be downloaded here). In Windows 7, you can:
put the program (.exe file) into the directory with raw files;
holding "Shift" key, right-click any empty place in the directory and select "Open command window here";
paste in the command window the string
dcraw-9.27-ms-32-bit.exe -v -t 0 -T -j -4 -D *.ARW (in 32-bit Windows)
or
.\dcraw-9.27-ms-64-bit.exe -v -t 0 -T -j -4 -D *.ARW (in 64-bit windows)
where *.ARW means processing of all ARW files in the folder (for processing a single file, replace these symbols with its name). Other cameras have other raw file extensions, for example, Canon cameras can have *.CR2;
In the mentioned string, command "-v" makes the program to display information about the work, "-t 0" ensures no rotation (ignoring of the camera's gravity sensor data), "-T" is for writing tiff files, "-j" for "not stretching or rotating raw pixels", "-4" for linear 16-bit output without white balance correction and "-D" for writing brightness values without scaling ("totally raw"). Full list of the incantations can be found here.
Calling dcraw from Matlab :
status_curr = system(['dcraw-9.27-ms-64-bit.exe -v -t 0 -T -j -4 -D ' '"' full_path_of_the_image '"']);
full_path_of_the_image must be a string containing full path with the filename of the raw image (e.g., 'G:\! my images\IMG_0011.CR2'). The file dcraw-9.27-ms-64-bit.exe must be located in the current folder used by Matlab (where the scripts are located). status_curr is "exit status of the command" (0 indicates success). Double quotes ensure correct handling of the paths containing spaces and Cyrillic symbols.
(Matlab R2013a, but not R2015b, seems to have some trouble with calling of dcraw).
The decoded file will be located in the same folder as the original.
After this, you will have tiff files with brightness values identical to original ones. They are still not viewable, because they are neither brightness-adjusted nor demosaiced, but now suitable for processing by tools like Matlab.
Short aside: complete conversion with dcraw
Dcraw can also convert raw files into ready viewable images (the same commands as above, but without "-4" and "-D"). For better results, you should also add some other things (otherwise the program uses proper settings, which can be nonoptimal). These things include, but are not limited to:
command -w, which tells the program to use white balance coefficients from the image's metadata;
file of the dark frame (example of the command: -K darkframe.PGM with uppercase "K") or a single number — mean brightness of such frame (-k 128 with lowercase "k");
saturation brightness (-S 4090).
Default dcraw's values for dark and saturation brightness of Sony NEX-5 are 116 and 4095 respectively. The above-mentioned values 128 and 4090 are actual values for my camera of this model (my report of this bug had no consequences). It's noteworthy that 4095 is 212-1, and 128 is 27. So, images of this camera are actually almost 12-bit.
So, the command for complete conversion should look like
dcraw-9.27-ms-32-bit.exe -v -t 0 -T -j -w -k 128 -S 4090 *.ARW
Default values of the parameters, used by dcraw, come from the program's code, which contains data for a huge number of cameras. For Sony NEX-5, it has even 2 sets of parameters (of which the first is used):
{ "Sony NEX-5", 116, 0, /* DJC */
{ 6807,-1350,-342,-4216,11649,2567,-1089,2001,6420 } },
...
{ "Sony NEX-5", 0, 0, /* Adobe */
{ 6549,-1550,-436,-4880,12435,2753,-854,1868,6976 } },
"116" stands for darkness level, and numbers "6807...6420" are coefficients of the XYZ-to-camera color transformation matrix.