You must first read your data from a text file into a matrix in MATLAB. Since I donβt know what format your text file is in, I can only assume that you probably need to use the fscanf function to read all of your values ββ(probably like uint16 ), then you probably have to change the values ββto N -M using the reshape function.
Suppose you did all this and now you have the N-by-M img matrix of unsigned 16-bit integers. First, you can use the bitand function to extract bits for the red, green, and blue components whose positions are in a 16-bit integer:

You can then use the bitshift function and multiply by a scaling factor to scale the red, green, and blue values ββto a range from 0 to 255, then convert them to an unsigned 8-bit integer using the uint8 function. This will give you three color component matrices of the same size as img :
imgR = uint8((255/31).*bitshift(bitand(img, 63488), -11)); % Red component imgG = uint8((255/63).*bitshift(bitand(img, 2016), -5)); % Green component imgB = uint8((255/31).*bitand(img, 31)); % Blue component
Now you can use the cat function to place the three color components in the N-by-M-by-3 RGB bit, then save the image in the RGB24 raster file using the imwrite function:
imgRGB = cat(3, imgR, imgG, imgB); % Concatenate along the third dimension imwrite(imgRGB, 'myImage.bmp'); % Output the image to a file
Example:
Using a randomly generated matrix of 100 to 100 uint16 values ββand applying the above transforms, here are the results:
img = randi([0 65535], 100, 100, 'uint16'); % Perform the above conversions to get imgRGB subplot(1, 2, 1); imshow(img); title('Random uint16 image'); subplot(1, 2, 2); imshow(imgRGB); title('Corresponding RGB image');
