Thursday, November 17, 2011

Creating awesome functions in Matlab is easy

When you open Matlab you will see Editor window, Command window, Workspace, Command history etc. See the screenshot attached here.



Functions


A function is a set of commands. A function is written in an editor window. For our first function let's write a protocol to display an image.


function practical(image)
A=imread(image);
figure;
imshow(A);
end



Here the name of the function is "practical" the input argument is "image".
The name of the file must match the name of the function. Thus you save this file as practical.m
The function is terminated with the keyword "end". This is optional if you have only one function in the file.


Running the function


To run this function write the following in the command window.

>> practical('mri.jpg')

Now, this assumes that the image mri.jpg is in your working directory.


Making your code childproof 


You see, the code that you write is not for you. Someone else is going to use it. The end user is most likely not a programmer, he is a normal person... Usually the functions that you would create will be pretty long. The user doesn't have the time to read what the function does. How it works, how to run it, etc. As a good programmer your goal should be to write a code such that anyone can run it. One can not make assumptions about the knowledge of the user.


Let's see what more the above code assumes.


1. First it assumes that image is the name of the image that you want to display. Well, what if it is already a variable that some other code has stored the image in?


2. It assumes that the user knows how to run the code in Matlab, i.e. by writing practical('mri.jpg') in the command window.  The user might just try to run the code by hitting the "run" button in the editor window.


3. It assumes that the image is uint8 or else the imshow will not work well.  As we noted before to work with images effectively I like to convert them to class double. But if you convert the image with class double, the imshow will give a silly output as it wants the function to range from 0 to 1. 


4. It assumes that the file 'mri.jpg' exists in the Matlab's path.


Childproof function


Below is a completely childproof code. It will work no matter what!



%==========================================================================



function practical(image)

% Author: Prashant Athavale
% This functions displays an image

%==========================================================================

clc; % this clears the Command Window

close all; % this closes previous figures if open

if nargin==0 % nargin gives the number of input argument
    image='mri.jpg'; % if the user gives no input arguments then
end

% let's see if the input is name of a file or is it a variable

if (ischar(image)&&(exist(image, 'file')==2))||(exist('image', 'var')==1)
    
    if (ischar(image)&&(exist(image, 'file')==2))
        A=double(imread(image));
    elseif (exist('image', 'var')==1)&&(ischar(image)==0)
        A=double(image);
    end
    
    if exist('A', 'var')
        size_of_the_image=size(A);
        size_of_size=size(size_of_the_image);
    end
end


if exist('A', 'var')&&(size_of_size(2)==3) % i.e. if the image is color, or RGB image
    if max(max(max(A)))>1
        M=255;
    else
        M=1;
    end
    figure;
    imshow(A/M);
elseif exist('A', 'var')&&(size_of_size(2)==2) % i.e. if the image is  grayscale image
    if max(max(A))>1
        M=255;
    else
        M=1;
    end
    figure;
    imshow(A, [0, M]);
end


if exist('A', 'var')==0
    if (ischar(image))
        text=['The input file: ' image ' does not exist in current directory.'];
        errordlg(text);
    else
        text='The input does not exist in current directory.';
        errordlg(text);
    end
end

end % end of the function practical

%==========================================================================





No comments:

Post a Comment