Bowecho
Bowecho

Reputation: 909

Reading a raw IMG file in MATLAB

I am trying a code to help me read a raw IMG file, but I get an error. My code is the following:

function image = readrawimage('/Users/Adrian/Documents/MATLAB/HiWinGS wind MATLAB 09_30_13/00006010.img')
fid = fopen('/Users/Adrian/Documents/MATLAB/HiWinGS wind MATLAB 09_30_13/00006010.img');
image = fread(fid, 2048*2048, 'uint8=>uint8');
fclose(fid);
image = reshape(image, 2048, 2048);

And I get the following error:

Undefined function 'readrawimage' for input arguments of type 'char'.

I tried to change the path of my IMG file, but it's still doesn't work. Does anyone knows a way to fix this error? Thank you.

Upvotes: 0

Views: 170

Answers (1)

Juderb
Juderb

Reputation: 735

Matlab is not recognizing your function in its current working path. You need to save the function in an m file named readrawimage.m, and make sure MATLAB can reach the function.

You can do this several ways:

  • Navigate to the directory containing your function, then run it as called.

  • Add the directory to MATLAB's search path interactively by calling pathtool()

  • Or add it programmatically by calling addpath('your directory')

For more on viewing and changing the search path, see http://www.mathworks.com/help/matlab/search-path.html

The MATLAB search path is described here: http://www.mathworks.com/help/matlab/matlab_env/what-is-the-matlab-search-path.html


I would also like to add that your function is not defined properly. You need to declare an input argument in the function declaration, then define that argument in your function call. So, your function should be defined and saved in an .m file as:

function img = readrawimage(filePath)
  fid = fopen(filePath);
  image = fread(fid, 2048*2048, 'uint8=>uint8');
  fclose(fid);
  image = reshape(image, 2048, 2048);
end

And you should call the function in the command line, or another script or function, by:

img = readrawimage('your path'); 

Upvotes: 4

Related Questions