Digital Image Processing
Scripts, Functions & P-codes in MATLAB
Dr. Zohair Al-Ameen
M-File Scripts
• The problem with Command Window
commands is they cannot be saved and
executed again for several times.
• To execute commands repeatedly, do:
– Create a file with a list of commands.
– Save the file.
– Run the file.
• The files that are used for this purpose are
called script files or scripts for short.
M-File Scripts
• A script file is an external file that contains a
sequence of MATLAB statements.
• Script files have a filename extension .m
and are often called M-files.
• M-files can be scripts that simply execute a
series of MATLAB statements.
• M-files can be functions that can accept
arguments and can produce one or more
outputs.
Script side-effects
• All variables created in a script file are
added to the workspace.
• This may have undesirable effects,
because:
– Variables already existing in the
workspace may be overwritten.
Functions in Computer Programming
• A function is a block of organized, reusable code
that is used to perform a designated action.
• Its concept is simple. You write your function that
contains the desired code and then call it in the main
program.
• By doing so, you get the following benefits:
– Your will allow code reusability.
– Your code will become well-organized.
– You will easily detect errors.
– You will easily modify your code.
– You will have less code lines.
M-File functions
• functions are programs (or routines)
that accept input arguments and
return output arguments.
• Each M-file function (or function
or M-file for short) has its own area
of workspace, separated from the
MATLAB base workspace.
Input and output arguments
• The general form of a MATLAB function is:
function [outputs] = function_name(inputs)
• A function file can have one or several output
arguments.
Anatomy of a M-File function
• The basic parts of an M-file.
• function f = factorial(n) (1)
• % FACTORIAL(N) returns the factorial of N. (2)
• % Compute a factorial value. (3)
• f = prod(1:n); (4)
• As an example, for n = 5, the result is,
>> f = factorial(5)
f=
120
Anatomy of a M-File function
• Both functions and scripts can have all of these
parts, except for the function definition line which
applies to function only.
Anatomy of a M-File function
• The characteristics of a MATLAB
function name:
– Must begin with a letter
– Must be no longer than 63 characters.
– The name of the text file that you save will
consist of the function name with the
extension .m.
– The previous example file name would be
factorial.m.
Anatomy of a M-File function
• Summarizes the differences between:
Scripts and Functions
P-codes in Matlab
• Although an (.m) file is executable by itself, the
contents of the source files are easily accessed,
revealing design and implementation details.
• If you want other people to run your code but you
do not want them to see its contents, you can
convert your code as p-code.
• The way to do so is to create a function that has
the code and create another script that runs this
function.
• The function itself can then be converted to p-code.
P-codes in Matlab
• P-code means content-obscured format of the (.m) file.
• P-code file owns a (.p) file extension.
• When you convert your file to P-code format, the file contents
becomes obfuscated not encrypted.
• The P-code file also runs at the same speed as the source file.
• To generate a P-code file, enter the following command in the
MATLAB Command Window:
pcode file_name.m
• Your .m file should be in the active work directory (folder).
• Your newly created P-code file will be in the same directory
(folder).
• Remember: P-code files are always functions; just call them the
same way as you would any ordinary function.
THE END