1

Since it is not possible to have a script and a function definition in the same file , I thought to echo the function which I want to attach in the script such that I get a script with function code and then some usage with this function .

For example -

func1.m

function [result] = func1(x)
    result=sqrt(x) ;  
end

script1.m

echo(func1.m) ; 
display(func1(9))  

Desire output for script1.m

function [result] = func1(x)
        result=sqrt(x) ;  
    end
display(func1(9)) 
3

Have you any idea for that ?

Community
  • 1
  • 1
URL87
  • 10,667
  • 35
  • 107
  • 174
  • 1
    -1: why on earth would you want to do this? Other than classroom use I really don't see how this could be useful, and even there; why not just open the editor? – Rody Oldenhuis Jun 26 '13 at 13:13
  • @RodyOldenhuis : you right , it's for classroom use - I should write a function and then show how I used it , so I create a script which I write it `type` of the function then some usage with this function and finally export all its output into pdf using `publish` option . – URL87 Jun 27 '13 at 09:53

3 Answers3

7

Since a convoluted solution was already proposed, why not stating the obvious?

Matlab has a built-in command that does exactly what you want. It is called type:

>> type('mean')

will give you this:

function y = mean(x,dim)
%MEAN   Average or mean value.
%   For vectors, MEAN(X) is the mean value of the elements in X. For
%   matrices, MEAN(X) is a row vector containing the mean value of
%   each column.  For N-D arrays, MEAN(X) is the mean value of the
%   elements along the first non-singleton dimension of X.
%
%   MEAN(X,DIM) takes the mean along the dimension DIM of X. 
%
%   Example: If X = [0 1 2
%                    3 4 5]
%
%   then mean(X,1) is [1.5 2.5 3.5] and mean(X,2) is [1
%                                                     4]
%
%   Class support for input X:
%      float: double, single
%
%   See also MEDIAN, STD, MIN, MAX, VAR, COV, MODE.

%   Copyright 1984-2005 The MathWorks, Inc. 
%   $Revision: 5.17.4.3 $  $Date: 2005/05/31 16:30:46 $

if nargin==1, 
  % Determine which dimension SUM will use
  dim = min(find(size(x)~=1));
  if isempty(dim), dim = 1; end

  y = sum(x)/size(x,dim);
else
  y = sum(x,dim)/size(x,dim);
end
Shai
  • 111,146
  • 38
  • 238
  • 371
1

You could use this:

function echo(mfile)
    filename=which(mfile);
    if isempty(filename)
        fprintf('Invalid input - check you are inputting a string.');
        return;
    end
    fid=fopen(filename,'r');
    if (fid<0)
        fprintf('Couldn''t open file.');
    end
    file=fread(fid,Inf);
    fclose(fid);
    fprintf('%s',file);
end

This will open a file, read it, and print it. Note that you need to provide the input as a string, i.e. with single quotes around it, and need to have '.m' at the end:

echo('fread.m')

Not

echo(fread.m) % This won't work
echo('fread') % This won't work
Shai
  • 111,146
  • 38
  • 238
  • 371
Hugh Nolan
  • 2,508
  • 13
  • 15
  • @Shai : I just tried it now . excellent too ! so the function of HughNolan is already exists .... – URL87 Jun 26 '13 at 09:16
1

Just for completeness, there's also dbtype which prepends line numbers.

Edric
  • 23,676
  • 2
  • 38
  • 40