Wednesday, December 23, 2009

MATLAB gem II: An example

While I was ‘playing’ with the function fdep I decided to write a small M-file to show usage and potential problem.

The code does not produce any useful results but it is not junk either. It calls functions from a few toolboxes. Each function uniquely appears in each toolbox; No ambiguity. Then there is a parameter whose name shadows the name of a MATLAB function.

%% Quick code to test fdep

clear all

% Create 0-mean normal distribution with std = 2
Y = 0 + 2.* randn(1,128);

n = length(Y); % Length of vector n

yy = ceil( max( abs(Y) ) ); % max abs value of data

%% Run sequence plot - MATLAB

subplot(221)
plot(Y, '-')
grid on
axis([1 Inf -yy yy])
title('Run Sequence plot')

%% decimate - Signal Processing Toolbox

YY = decimate(Y,2);

subplot(222)
plot(linspace(1,128,length(YY)), YY, '-')
grid on
axis([1 Inf -yy yy])
title('Decimated Run Sequence plot')

%% Create histogram - MATLAB + Statistics Toolbox

subplot(223)
hist(Y)
hold on
histfit(Y)
hold off
axis([-yy yy 0 Inf])
title('Histogram')

%% Normal probability plot - Statistics Toolbox

subplot(224)
normplot(Y)
title('Normal Probability Plot')

%% Unrelated code: Use parameter named 'step' – MATLAB

lamda = 100;

K = 154;

step = 0;
count = 0;

for id = 1:K+1
   dummy = exp(-lamda) *lamda^(id-1) /factorial(id-1);
   step = step + dummy;
   count = count + 1;
end

The code results in the following plot:

testCode_0912_01

The code is actually 3/4 useful but that is another story.

Now, if you execute fdep from the command line on the M-file shown above the output would look like:

>> P = fdep('testCode_0912_01');
ROOT   : > D:/Documents and Settings/../My Documents/MATLAB/Testing Ground/testCode_0912_01.m
1    1   : | testCode_0912_01      S  0:

-------------  NO USER-DEFINED DEPENDENCIES FOUND
M-FILE       : D:/Documents and Settings/../My Documents/MATLAB/Testing Ground/testCode_0912_01.m
P-FILE       :                                                                                       
MODULE  #   1: testCode_0912_01                                                                      
type         : SCRIPT                                                                                
created      : 23-Dec-2009 12:30:36                                                                  
size         : 1046 bytes                                                                            
lines        : 62                                                                                    
comments     : 9                                                                                     
empty        : 21                                                                                    
recursive    : no                                                                                    
f/eval..     : not used                                                                              
unresolved   : no                                                                                    
calls    TO  :     0  user defined                                                                   
called   FROM:     0  user defined                                                                   
calls in FILE:    20                                                                                 
subfunctions :     0  inside  file                                                                   
nested       :     0                                                                                 
anonymous    :     0                                                                                 
f/eval..     :     0                                                                                 
unresolved   :     0                                                                                 
ML      stock:    13                                                                                 
ML  built-ins:    10                                                                                 
ML    classes:     1                                                                                 
OTHER classes:     0                                                                                 
ML  toolboxes:     4                                                                                 
             :    +1  Control System Toolbox (8.2) [control]                                         
             :    +2  MATLAB (7.7) [matlab]                                                          
             :    +3  Signal Processing Toolbox (6.10) [signal]                                      
             :    +4  Statistics Toolbox (7.0) [stats]                                               
>>

>> P.toolbox

ans =

    'Control System Toolbox (8.2) [control]'
    'MATLAB (7.7) [matlab]'
    'Signal Processing Toolbox (6.10) [signal]'
    'Statistics Toolbox (7.0) [stats]'

>>

Mostly correct; Unfortunately it identifies the presence of the Control Systems Toolbox because of the presence of parameter step. Can’t have everything! Still, a very useful function.

 

Technorati Tags: ,,,,,,

No comments:

Post a Comment