Clear Filters
Clear Filters

Trying to make a recursive tree function that calls on itself using OOP, where the object is a turtle.

3 views (last 30 days)
I have a separate turtle class created already. Here is my code.
function [obj] = tree(obj,n) % Creates tree to the order n
obj = Turtle();
t = Turtle();
a = 60; % angle in degrees
d = 4; % distance turtle travels
% If n is 0,
if n <= 0
% do nothing.
t = t.fd(0);
% Otherwise,
else
% drive forward d,
t = t.fd(d);
% turn left a degrees,
t = t.lt(a);
% draw a level n-1 tree,
obj = tree(n-1)
% turn right 2a degrees,
t = t.rt(2*a);
% draw a level n-1 tree,
obj = tree(n-1)
% turn left a degrees,
t = t.lt(a);
% and drive backward d
t = t.bk(d);
end
end
Matlab gives me an error saying "Undefined function or variable 'Turtle'". Does anyone know how to fix this?
  1 Comment
Geoff Hayes
Geoff Hayes on 18 Oct 2016
Kyle - you indicate that you want to make a recursive tree function that calls on itself but your function tree never calls itself. It does call the function (or is this an object that you are instantiating?) called Turtle. The MATLAB error message is indicating that this function (or class) cannot be found within the MATLAB search path. Is Turtle something that you have written? If so, then you need to add this file to the search path so that your tree function can find it.

Sign in to comment.

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!