Loren on the Art of MATLAB

Turn ideas into MATLAB

Including External Code in Published Document 10

Posted by Loren Shure,

When I wanted to show you a code snippet in the past, I displayed the code in the text of my blog post.

Contents

How I Did It

To do so, I often used the function type, or sometimes dbtype (if I wanted to refer to line numbers). In this way, I did not require you to download code from elsewhere to see what I was discussing.

How I Do It Now

Now using R2015a I can take advantage of a new feature of the markup language for publish. Using the include markup allows me to include external content. How do I do this? With the delimiters

             <include>fileOfInterest.m</include>

Not only that, but the content is included with the proper syntax highlighting for MATLAB code!

Let's Try It Out!

Here's some code from an older post of mine of methods for computing Fibonacci numbers.

function f = fibrec(n)
%FIBREC Recursive function for n Fibonacci numbers.

% Minimize the error checking so we don't bog down in it. I have included it
%in comments instead.
% if ~isscalar(n) | ~isreal(n) | n<0 | fix(n)~=n
%    error('ArtBlog:fibrec:MBRealPosInt','N must be a real positive integer')
% end
if n == 1,
    f = 1;   % First element is 1.
    return;
elseif n == 2
    f = [1 1];  % First two elements are 1.
else
    % Call fibrec with previous result and compute next one from it.
    fm1 = fibrec(n-1); 
    f = [fm1 fm1(end)+fm1(end-1)];
end

Will This Inclusive Feature Help You?

Let us know how here.


Get the MATLAB code

Published with MATLAB® R2015a

10 views (last 30 days)  | |

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.