Search code examples
matlabsubplot

Not able to resolve Error in subplot function


This is my code:

subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
title('Subplot 1: sin(x)')

subplot(2,2,2)
y2 = sin(2*x);
plot(x,y2)
title('Subplot 2: sin(2x)')

subplot(2,2,3)
y3 = sin(4*x);
plot(x,y3)
title('Subplot 3: sin(4x)')

subplot(2,2,4)
y4 = sin(8*x);
plot(x,y4)
title('Subplot 4: sin(8x)')

But I am getting this error: Execution of script subplot as a function is not supported: /MATLAB Drive/subplot.m

My file is named 'hello.m'

Please help me resolve this error.


Solution

  • This error means that you are overriding the built-in subplot function with a script you created with the same name, subplot.m, and is present on the path.

    I would suggest to run:

    which subplot

    in matlab command window, this will return the path to the file/function that matlab is using, most probably in your case the custom made one, not the built-in.

    The built-in path should look more or less like this:

    ...\MATLAB\R20xxy_x64\toolbox\matlab\graph2d\subplot.m where 20xxy is your version (ie 2022a)

    If you are overriding it will return the path to you script instead, .. /MATLAB Drive/subplot.m

    The solution is to rename your script. I would strongly advice to avoid renaming or modifying the built-in function