# Matlab says, I reached the max limit of recursion: " Maximum recursion limit of 500 reached. "

3 views (last 30 days)
Stephen on 23 Jul 2021
Commented: Image Analyst on 23 Jul 2021
Here's my code from Help Manual:
x = randn(100,1);
y = randn(100,1);
Xedges = [-Inf -2:0.4:2 Inf];
Yedges = [-Inf -2:0.4:2 Inf];
h = Histogram2(x,y,Xedges,Yedges)
Apparently there's some other conditions that need to be established before I can run this example code from Help manual, but being new to Matlab, I can' figure this out.
Thanks,
Stephen
Image Analyst on 23 Jul 2021
@Chunru, like I said in my second answer below, if newfunction is the name of his m-file, there may be only one, but it's being called recursively. Fixes are in my answer below.

Image Analyst on 23 Jul 2021
newfunction()
x = randn(100,1);
y = randn(100,1);
Xedges = [-Inf -2:0.4:2 Inf];
Yedges = [-Inf -2:0.4:2 Inf];
h = histogram2(x,y,Xedges,Yedges)
So, that's the full code? Your m-file is not by any chance called "newfunction.m" is it? Because the first line of newfunction would call itself recursively, like I tried explain to you in my first answer. To fix, either comment out that first line,
% newfunction()
x = randn(100,1);
y = randn(100,1);
Xedges = [-Inf -2:0.4:2 Inf];
Yedges = [-Inf -2:0.4:2 Inf];
h = histogram2(x,y,Xedges,Yedges)
or put the function keyword in front of it
function newfunction()
x = randn(100,1);
y = randn(100,1);
Xedges = [-Inf -2:0.4:2 Inf];
Yedges = [-Inf -2:0.4:2 Inf];
h = histogram2(x,y,Xedges,Yedges)
##### 2 CommentsShowHide 1 older comment
Image Analyst on 23 Jul 2021
You're welcome. Thanks for accepting the answer.

Image Analyst on 23 Jul 2021
Once I changed Histogram2 to histogram2 (MATLAB is case sensitive), it works fine:
x = randn(100,1);
y = randn(100,1);
Xedges = [-Inf -2:0.4:2 Inf];
Yedges = [-Inf -2:0.4:2 Inf];
h = histogram2(x,y,Xedges,Yedges)
I suspect that your m-file is called Histogram2.m and that it's calling itself. It recurses deeper and deeper with no way to back out, until it eventually calls itself 500 times and throws that error you saw.
Correct the name to histogram2, and change the name of your m-file to something like test_histogram.m instead of Histogram2.m.

Chunru on 23 Jul 2021
Try "clear all" before running the code. Change "Histogram2" to "histogram2" (case sensitive). It seems there is no recursive function in the code. If the problem cannot be resolved, do "dbstop error" before running the code and observe the error message.
x = randn(100,1);
y = randn(100,1);
Xedges = [-Inf -2:0.4:2 Inf];
Yedges = [-Inf -2:0.4:2 Inf];
h = histogram2(x,y,Xedges,Yedges);

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by