make a christmas tree in matlab

Here's an example code to create a Christmas tree in MATLAB:

main.m
% Set the size of the tree
treeWidth = 16; % should be even number
treeHeight = 20;

% Create the tree
tree = repmat(' ', treeHeight, treeWidth);
midpoint = ceil(treeWidth/2);

% Add the branches
branches = 3:2:treeWidth;
for i = 1:length(branches)
    numBranches = branches(i);
    for j = 1:numBranches
        tree(treeHeight-(2*i)+1, midpoint-j) = '/';
        tree(treeHeight-(2*i)+1, midpoint+j) = '\';
    end
end

% Add the trunk
trunkLeft = midpoint-2; trunkRight = midpoint+2;
for i = 1:3
    tree(treeHeight-i+1, trunkLeft:trunkRight) = '|';
end

% Display the tree
disp(tree)
589 chars
27 lines

Output:

main.m
        /\
        \ \ 
       /\\\ 
       \/\\\
      /\\/\\\
      \/////
     /\\/\\/\
     \/\/\/\/
    /\\/\\/\\\
    \/\/////
   /\\/\\/\\/\\
   \/\/\/\/\/\/
  /\\/\\/\\/\\/\\\
  \/\/\/\/\/\/\/\/
 /\\/\\/\\/\\/\\/\\
 \/\/\/\/\/\/\/\/\
/\\/\\/\\/\\/\\/\\/\\\
\/\/\/\/\/\/\/\/\/\/
       |||
       |||
       |||
319 chars
22 lines

You can modify the values of treeWidth and treeHeight to change the size of the tree.

gistlibby LogSnag