How to solve "Not Enough Input Arguments" error in a recursive function?

2 visualizzazioni (ultimi 30 giorni)
The goal of my program is to create Sierpinski Carpet. When i run the code i get the "Not Enough Input Arguments" error.
function out= MyFunction(x, y ,width, depth, max_depth)
clc
if depth > max_depth
rectangle('Position',[x, y, width, width]);
else
width=width/3;
MyFunction(x, y, width, depth+1);
MyFunction(x+width, y, width, depth+1);
MyFunction(x+width+width, y, width, depth+1);
MyFunction(x, y+width, width, depth+1);
%MyFunction(x, y+width, width, depth+1); %middle empty
MyFunction(x+width+width, y+width, width, depth+1);
MyFunction(x, y+width+width, width, depth+1);
MyFunction(x+width, y+width+width, width, depth+1);
MyFunction(x+width+width, y+width+width, width, depth+1);
end
end
When i call the function in console i get the error for example:
> MyFunction(0,0,9,0,3)

Risposta accettata

harsha001
harsha001 il 20 Mar 2019
Modificato: madhan ravi il 21 Mar 2019
You defined the function to have 5 arguments (in the first line) yet in your recursive calls, you are only passing 4 arguments.
You are missing a 'max_depth' argument in all your recursive calls to MyFunction in the 'else part':
function out= MyFunction(x, y ,width, depth, max_depth)
clc
if depth > max_depth
rectangle('Position',[x, y, width, width]);
else
width=width/3;
MyFunction(x, y, width, depth+1, max_depth);
MyFunction(x+width, y, width, depth+1, max_depth);
MyFunction(x+width+width, y, width, depth+1, max_depth);
MyFunction(x, y+width, width, depth+1, max_depth);
%MyFunction(x, y+width, width, depth+1); %middle empty
MyFunction(x+width+width, y+width, width, depth+1, max_depth);
MyFunction(x, y+width+width, width, depth+1, max_depth);
MyFunction(x+width, y+width+width, width, depth+1, max_depth);
MyFunction(x+width+width, y+width+width, width, depth+1, max_depth);
end
end

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by